来自另一个 class 的 setText 将打开另一个 windows
setText from another class will open another windows
(抱歉英语不好)
我正在尝试从另一个 class 编辑 JLabel,它成功了,但我发现一个问题,一旦它编辑文本,它将打开另一个 window。
如何解决?
代码:
Menu.java:
package Demo1;
import java.awt.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.Timer;
import javax.net.SocketFactory;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.synth.ColorType;
import java.awt.Window.Type;
public class Menu extends JFrame {
JFrame f;
public static void main(String argv[]){
new Menu();
Sdle();
}
public static void Sdle() {
Timer Timer = new Timer();
Timer.schedule(new ServerPing(), 1, 120000);
}
public Font nmFont(Integer Size) {
Font NewFont = new Font("TimesRoman", Font.PLAIN, Size);
return NewFont;
}
public void setSerLabel(String CT){
ServerStatusLb.setText(CT);
}
public void setClockLabel(String CT1){
ClockLb.setText(CT1);
}
private JLabel ClockLb = new JLabel("Timer",SwingConstants.CENTER);
private JLabel ServerStatusLb = new JLabel("New label",SwingConstants.CENTER);
public Menu(){
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("Menu");
f.setResizable(false);
f.setSize(470,345);
f.setLocationRelativeTo(null);
f.setVisible(true);
Container cp=f.getContentPane();
cp.setLayout(null);
ClockLb.setBounds(0, 0, 454, 75);
ClockLb.setFont(nmFont(66));
cp.add(ClockLb);
ServerStatusLb.setBounds(153, 79, 160, 31);
ServerStatusLb.setFont(nmFont(20));
cp.add(ServerStatusLb);
}
}
和ClockMa.java(管理时钟标签):
package Demo1;
import java.text.*;
import java.util.*;
public class ClockMa extends TimerTask {
@Override
public void run() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //可用: YY, MM, DD, HH, mm, ss
Date date = new Date();
Menu MN = new Menu();
MN.setClockLabel(dateFormat.format(date));
}
}
new Menu()
每次运行 run()
都会创建一个新菜单。相反,在 run()
操作之外创建一个菜单实例,并在 run()
操作
中设置标签文本
(抱歉英语不好)
我正在尝试从另一个 class 编辑 JLabel,它成功了,但我发现一个问题,一旦它编辑文本,它将打开另一个 window。
如何解决?
代码:
Menu.java:
package Demo1;
import java.awt.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.Timer;
import javax.net.SocketFactory;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.synth.ColorType;
import java.awt.Window.Type;
public class Menu extends JFrame {
JFrame f;
public static void main(String argv[]){
new Menu();
Sdle();
}
public static void Sdle() {
Timer Timer = new Timer();
Timer.schedule(new ServerPing(), 1, 120000);
}
public Font nmFont(Integer Size) {
Font NewFont = new Font("TimesRoman", Font.PLAIN, Size);
return NewFont;
}
public void setSerLabel(String CT){
ServerStatusLb.setText(CT);
}
public void setClockLabel(String CT1){
ClockLb.setText(CT1);
}
private JLabel ClockLb = new JLabel("Timer",SwingConstants.CENTER);
private JLabel ServerStatusLb = new JLabel("New label",SwingConstants.CENTER);
public Menu(){
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("Menu");
f.setResizable(false);
f.setSize(470,345);
f.setLocationRelativeTo(null);
f.setVisible(true);
Container cp=f.getContentPane();
cp.setLayout(null);
ClockLb.setBounds(0, 0, 454, 75);
ClockLb.setFont(nmFont(66));
cp.add(ClockLb);
ServerStatusLb.setBounds(153, 79, 160, 31);
ServerStatusLb.setFont(nmFont(20));
cp.add(ServerStatusLb);
}
}
和ClockMa.java(管理时钟标签):
package Demo1;
import java.text.*;
import java.util.*;
public class ClockMa extends TimerTask {
@Override
public void run() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //可用: YY, MM, DD, HH, mm, ss
Date date = new Date();
Menu MN = new Menu();
MN.setClockLabel(dateFormat.format(date));
}
}
new Menu()
每次运行 run()
都会创建一个新菜单。相反,在 run()
操作之外创建一个菜单实例,并在 run()
操作