我有一个类TabBuilder
,我用它来创建我的应用程序的接口,但我遇到了一个奇怪的问题。如果我尝试运行下面的代码,它将绘制 mainScreen,但如果我从 BarMenu 请求 SearchScreen,它不会显示。如果我尝试在public static void main (String[] args)
MainString 中仅执行 SearchScreen(调用其构建器),它也不会显示。但是如果转到请求事件并提示TabBuilder tb = new TabBuilder(); tb.requestTab();
屏幕将按原样显示。那么,可能出了什么问题?提前致谢
主屏幕:
public class MainScreen{
public MainScreen()
{
TabBuilder tb = new TabBuilder();
tb.mainTab();
}
}
搜索屏幕:
public class SearchScreen{
public void SearchScreen(){
TabBuilder tb = new TabBuilder();
tb.requestTab();
}
}
选项卡生成器:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TabBuilder implements ActionListener {
JTabbedPane tabbedPane = new JTabbedPane();
JMenuItem close, request;
protected JTextField txtrequest = new JTextField();
JButton btrequest = new JButton();
protected JFrame requestFrame = new JFrame();
public void TabBuilder(){
}
public void mainTab(){
JMenuBar bar;
JMenu file, register;
JFrame mainFrame = new JFrame();
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Request");
close= new JMenuItem("Close");
close.addActionListener(this);
request= new JMenuItem("Request Query");
request.addActionListener(this);
bar.add(file);
bar.add(register);
file.add(close);
register.add(request);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void requestTab(){
JLabel lbrequest;
JPanel requestPane;
btrequest= new JButton("request");
lbrequest= new JLabel("Type Keywords in english to be requested below:");
txtrequest= new JTextField();
requestPane=new JPanel();
requestPane.setBackground(Color.gray);
requestPane.add(lbrequest);
requestPane.add(txtrequest);
requestPane.add(btrequest);
requestPane.setLayout(new GridLayout(3,3));
btrequest.setEnabled(true);
requestFrame.add(requestPane);
requestFrame.setTitle("SHST");
requestFrame.setSize(400, 400);
requestFrame.setVisible(true);
requestFrame.setDefaultCloseOperation(1);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==request){
TabBuilder tb = new TabBuilder();
tb.requestTab();
}
}
public static void main (String[] args){
MainScreen m = new MainScreen();
}
}