单击按钮打开新屏幕或页面(java-eclipse)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24147671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open new screen or page on button click (java-eclipse)
提问by AndroidNovice21
I have a home page with title and a few buttons I cannot get a new window to open when i click on the button. Here is the code i have for the home page aswell as the class with next screen i am attempting to open trimed for what seems relevant. The NewTicketWindow class is also attached it is plain at the moment. Any help is appreciated.
我有一个带有标题和几个按钮的主页,当我点击按钮时,我无法打开一个新窗口。这是我的主页以及下一个屏幕的类的代码,我试图打开修剪似乎相关的内容。NewTicketWindow 类也已附加,目前很简单。任何帮助表示赞赏。
public class Home
{
private JFrame frame;
JInternalFrame internalFrame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Home window = new Home();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Home()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title1 = new JLabel("City of Murphy");
JLabel title2 = new JLabel("Traffic Ticket Input System");
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
JButton payTicketButton = new JButton("Make a Payment");
JButton reportButtton = new JButton("Ticket Report");
JButton exitButton = new JButton("Exit");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}
second class (the screen i want to open upon newticket button being pressed
第二类(我想在按下 newticket 按钮时打开的屏幕
public class NewTicketWindow extends JFrame
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NewTicketWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
}
采纳答案by Ronn Wilder
just add these lines into your action performed code -
只需将这些行添加到您的操作执行代码中 -
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
回答by M Anouti
The ActionListener
of newTicketButton
should create the new frame by calling the constructor of NewTicketWindow (same thing you are doing in the main
of NewTicketWindow
):
该ActionListener
的newTicketButton
应该调用NewTicketWindow的构造函数创建新框架(同样的事情,你在做main
的NewTicketWindow
):
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
NewTicketWindow newTicketWindow = new NewTicketWindow();
newTicketWindow.setVisible(true);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
Also you need to add the newTicketButton to the home window:
您还需要将 newTicketButton 添加到主窗口:
frame.add(newTicketButton);
frame.add(newTicketButton);