JLabel 不显示
JLabel not show
我遇到了一些问题。
1.我想在退出前显示消息。单击按钮和消息显示,0.5 秒后程序退出并打开下一个程序。
2。我想改变颜色;在第一个应用程序中颜色是正确的,但在下一个应用程序中是默认颜色。
你能修改这个或告诉我一步一步如何做到这一点吗?谢谢你的帮助 :)。
第一个程序代码:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Start extends JFrame implements ActionListener
{
JButton Polski, English; //nazwy przycisków
JLabel jezyk, language;
static JLabel wybór;
public Start()
{
setSize(330,170);//rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
jezyk = new JLabel("Choose language:");
jezyk.setBounds(40,10,200,40);
add(jezyk);
Polski = new JButton("Polski");
Polski.setBounds(40,50,100,30);
add(Polski);
Polski.addActionListener(this);
English = new JButton("English");
English.setBounds(150,50,100,30);
add(English);
English.addActionListener(this);
wybór = new JLabel("Choose");
wybór.setBounds(40,90,400,30);
add(wybór);
}
public static void main(String[] args)
{
System.out.println("Choose language:");
Start okno1 = new Start();
okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object klik = e.getSource();
if(klik==Polski)
{
wybór.setText("Wybrałeś język Polski.".toString()); //I want to show this massage before exit, but it not show
System.out.println("Wybrałeś język Polski."); //This show corect in console
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==English)
{
wybór.setText("You have chosen English.".toString()); //I want to show this massage before exit, but it not show
System.out.println("You have chosen English."); //This show corect in console
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
第二个程序:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class czynnośćPL extends JFrame implements ActionListener
{
JButton pole, obwód, objętość; //nazwy przycisków
JLabel Wybór, oblicz;
public czynnośćPL()
{
setSize(400,200);//rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
setBackground(Color.blue);
oblicz = new JLabel("Oblicz:");
oblicz.setBounds(40,10,200,40);
add(oblicz);
pole = new JButton("pole");
pole.setBounds(40,50,100,30);
add(pole);
pole.addActionListener(this);
obwód = new JButton("obwód");
obwód.setBounds(150,50,100,30);
add(obwód);
obwód.addActionListener(this);
objętość = new JButton("objętość");
objętość.setBounds(260,50,100,30);
add(objętość);
objętość.addActionListener(this);
}
public static void main(String[] args)
{
Start okno1 = new Start();
okno1.getContentPane().setBackground(new Color(189,189,189));
okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object klik = e.getSource();
if(klik==pole)
{
System.out.println("Wybrałeś pole.");
try {
Thread.sleep(100);
this.setVisible(false);
new polePL().setVisible(true);//co ma open
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==obwód)
{
System.out.println("Wybrałeś obwód.");
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==objętość)
{
System.out.println("Wybrałeś objętość.");
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
您必须在特殊延迟后在 gui 线程上使用 javax.swing.Timer
到 运行 任务:
Timer timer = new Timer(milliseconds, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hideMessageDialog();
Start.this.setVisible(false);
new czynnośćPL().setVisible(true);
}
});
timer.setRepeats(false);
timer.start();
milliseconds
在执行第一个任务之前设置延迟,并定期执行任务之间的延迟。通过调用 timer.setRepeats(false);
你告诉 Timer
只执行你的任务一次。任务是 ActionListener
的 actionPerformed
方法。
要更改 JFrame 的背景颜色,您可以:
getContentPane().setBackground(Color.BLUE);
您在另一个主要方法中完成了此操作。完整 Start
class 是:
package com.Whosebug.main;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Start extends JFrame implements ActionListener {
JButton Polski, English; // nazwy przycisków
JLabel jezyk, language;
static JLabel wybór;
private JDialog messageDialog;
private JLabel messageLabel;
public Start() {
getContentPane().setBackground(Color.BLUE);
setSize(330, 170);// rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
messageDialog = new JDialog(this);
messageLabel = new JLabel("", JLabel.CENTER);
messageDialog.setSize(200, 200);
messageDialog.getContentPane().add(messageLabel);
jezyk = new JLabel("Choose language:");
jezyk.setBounds(40, 10, 200, 40);
add(jezyk);
Polski = new JButton("Polski");
Polski.setBounds(40, 50, 100, 30);
add(Polski);
Polski.addActionListener(this);
English = new JButton("English");
English.setBounds(150, 50, 100, 30);
add(English);
English.addActionListener(this);
wybór = new JLabel("Choose");
wybór.setBounds(40, 90, 400, 30);
add(wybór);
}
public static void main(String[] args) {
System.out.println("Choose language:");
Start okno1 = new Start();
okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object klik = e.getSource();
if (klik == Polski) {
wybór.setText("Wybrałeś język Polski.".toString());
hideThisShowNewFrameAfterDelay(1500);
showMessageDialog("Wybrałeś język Polski");
System.out.println("Wybrałeś język Polski.");
}
else if (klik == English) {
wybór.setText("You have chosen English.".toString());
hideThisShowNewFrameAfterDelay(1500);
showMessageDialog("You have chosen English.");
System.out.println("You have chosen English.");
}
}
private void showMessageDialog(String message) {
messageLabel.setText(message);
messageDialog.setVisible(true);
}
private void hideMessageDialog() {
messageDialog.dispose();
}
private void hideThisShowNewFrameAfterDelay(int milliseconds) {
Timer timer = new Timer(milliseconds, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hideMessageDialog();
Start.this.setVisible(false);
new czynnośćPL().setVisible(true);
}
});
timer.setRepeats(false);
timer.start();
}
}
非常感谢,现在一切正常:),我很高兴:)
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Start extends JFrame implements ActionListener
{
JButton Polski, English; //nazwy przycisków
JLabel jezyk, language, wybór;
public Start()
{
getContentPane().setBackground(new Color (189, 189, 189));
setSize(330,170);//rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
jezyk = new JLabel("Choose language:");
jezyk.setBounds(40,10,200,40);
add(jezyk);
Polski = new JButton("Polski");
Polski.setBounds(40,50,100,30);
add(Polski);
Polski.addActionListener(this);
English = new JButton("English");
English.setBounds(150,50,100,30);
add(English);
English.addActionListener(this);
wybór = new JLabel("Choose");
wybór.setBounds(40,90,400,30);
add(wybór);
}
public static void main(String[] args)
{
System.out.println("Choose language:");
Start okno = new Start();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object klik = e.getSource();
if(klik==Polski)
{
wybór.setText("Wybrałeś język Polski.".toString());
System.out.println("Wybrałeś język Polski.");
super.update(this.getGraphics());
try {
Thread.sleep(600);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==English)
{
wybór.setText("You have chosen English.".toString());
System.out.println("You have chosen English.");
super.update(this.getGraphics());
try {
Thread.sleep(600);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
我遇到了一些问题。
1.我想在退出前显示消息。单击按钮和消息显示,0.5 秒后程序退出并打开下一个程序。
2。我想改变颜色;在第一个应用程序中颜色是正确的,但在下一个应用程序中是默认颜色。
你能修改这个或告诉我一步一步如何做到这一点吗?谢谢你的帮助 :)。
第一个程序代码:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Start extends JFrame implements ActionListener
{
JButton Polski, English; //nazwy przycisków
JLabel jezyk, language;
static JLabel wybór;
public Start()
{
setSize(330,170);//rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
jezyk = new JLabel("Choose language:");
jezyk.setBounds(40,10,200,40);
add(jezyk);
Polski = new JButton("Polski");
Polski.setBounds(40,50,100,30);
add(Polski);
Polski.addActionListener(this);
English = new JButton("English");
English.setBounds(150,50,100,30);
add(English);
English.addActionListener(this);
wybór = new JLabel("Choose");
wybór.setBounds(40,90,400,30);
add(wybór);
}
public static void main(String[] args)
{
System.out.println("Choose language:");
Start okno1 = new Start();
okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object klik = e.getSource();
if(klik==Polski)
{
wybór.setText("Wybrałeś język Polski.".toString()); //I want to show this massage before exit, but it not show
System.out.println("Wybrałeś język Polski."); //This show corect in console
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==English)
{
wybór.setText("You have chosen English.".toString()); //I want to show this massage before exit, but it not show
System.out.println("You have chosen English."); //This show corect in console
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
第二个程序:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class czynnośćPL extends JFrame implements ActionListener
{
JButton pole, obwód, objętość; //nazwy przycisków
JLabel Wybór, oblicz;
public czynnośćPL()
{
setSize(400,200);//rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
setBackground(Color.blue);
oblicz = new JLabel("Oblicz:");
oblicz.setBounds(40,10,200,40);
add(oblicz);
pole = new JButton("pole");
pole.setBounds(40,50,100,30);
add(pole);
pole.addActionListener(this);
obwód = new JButton("obwód");
obwód.setBounds(150,50,100,30);
add(obwód);
obwód.addActionListener(this);
objętość = new JButton("objętość");
objętość.setBounds(260,50,100,30);
add(objętość);
objętość.addActionListener(this);
}
public static void main(String[] args)
{
Start okno1 = new Start();
okno1.getContentPane().setBackground(new Color(189,189,189));
okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object klik = e.getSource();
if(klik==pole)
{
System.out.println("Wybrałeś pole.");
try {
Thread.sleep(100);
this.setVisible(false);
new polePL().setVisible(true);//co ma open
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==obwód)
{
System.out.println("Wybrałeś obwód.");
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==objętość)
{
System.out.println("Wybrałeś objętość.");
try {
Thread.sleep(100);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
您必须在特殊延迟后在 gui 线程上使用 javax.swing.Timer
到 运行 任务:
Timer timer = new Timer(milliseconds, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hideMessageDialog();
Start.this.setVisible(false);
new czynnośćPL().setVisible(true);
}
});
timer.setRepeats(false);
timer.start();
milliseconds
在执行第一个任务之前设置延迟,并定期执行任务之间的延迟。通过调用 timer.setRepeats(false);
你告诉 Timer
只执行你的任务一次。任务是 ActionListener
的 actionPerformed
方法。
要更改 JFrame 的背景颜色,您可以:
getContentPane().setBackground(Color.BLUE);
您在另一个主要方法中完成了此操作。完整 Start
class 是:
package com.Whosebug.main;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Start extends JFrame implements ActionListener {
JButton Polski, English; // nazwy przycisków
JLabel jezyk, language;
static JLabel wybór;
private JDialog messageDialog;
private JLabel messageLabel;
public Start() {
getContentPane().setBackground(Color.BLUE);
setSize(330, 170);// rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
messageDialog = new JDialog(this);
messageLabel = new JLabel("", JLabel.CENTER);
messageDialog.setSize(200, 200);
messageDialog.getContentPane().add(messageLabel);
jezyk = new JLabel("Choose language:");
jezyk.setBounds(40, 10, 200, 40);
add(jezyk);
Polski = new JButton("Polski");
Polski.setBounds(40, 50, 100, 30);
add(Polski);
Polski.addActionListener(this);
English = new JButton("English");
English.setBounds(150, 50, 100, 30);
add(English);
English.addActionListener(this);
wybór = new JLabel("Choose");
wybór.setBounds(40, 90, 400, 30);
add(wybór);
}
public static void main(String[] args) {
System.out.println("Choose language:");
Start okno1 = new Start();
okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object klik = e.getSource();
if (klik == Polski) {
wybór.setText("Wybrałeś język Polski.".toString());
hideThisShowNewFrameAfterDelay(1500);
showMessageDialog("Wybrałeś język Polski");
System.out.println("Wybrałeś język Polski.");
}
else if (klik == English) {
wybór.setText("You have chosen English.".toString());
hideThisShowNewFrameAfterDelay(1500);
showMessageDialog("You have chosen English.");
System.out.println("You have chosen English.");
}
}
private void showMessageDialog(String message) {
messageLabel.setText(message);
messageDialog.setVisible(true);
}
private void hideMessageDialog() {
messageDialog.dispose();
}
private void hideThisShowNewFrameAfterDelay(int milliseconds) {
Timer timer = new Timer(milliseconds, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hideMessageDialog();
Start.this.setVisible(false);
new czynnośćPL().setVisible(true);
}
});
timer.setRepeats(false);
timer.start();
}
}
非常感谢,现在一切正常:),我很高兴:)
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Start extends JFrame implements ActionListener
{
JButton Polski, English; //nazwy przycisków
JLabel jezyk, language, wybór;
public Start()
{
getContentPane().setBackground(new Color (189, 189, 189));
setSize(330,170);//rozmiar
setTitle("MathCalc v0.1 by Majkel");
setLayout(null);
jezyk = new JLabel("Choose language:");
jezyk.setBounds(40,10,200,40);
add(jezyk);
Polski = new JButton("Polski");
Polski.setBounds(40,50,100,30);
add(Polski);
Polski.addActionListener(this);
English = new JButton("English");
English.setBounds(150,50,100,30);
add(English);
English.addActionListener(this);
wybór = new JLabel("Choose");
wybór.setBounds(40,90,400,30);
add(wybór);
}
public static void main(String[] args)
{
System.out.println("Choose language:");
Start okno = new Start();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object klik = e.getSource();
if(klik==Polski)
{
wybór.setText("Wybrałeś język Polski.".toString());
System.out.println("Wybrałeś język Polski.");
super.update(this.getGraphics());
try {
Thread.sleep(600);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
else if(klik==English)
{
wybór.setText("You have chosen English.".toString());
System.out.println("You have chosen English.");
super.update(this.getGraphics());
try {
Thread.sleep(600);
this.setVisible(false);
new czynnośćPL().setVisible(true);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}