单击java gui中的按钮后如何在框架/类之间移动? [英] How to move between frames/classes after clicking a button in java gui?
本文介绍了单击java gui中的按钮后如何在框架/类之间移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Window.Type;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WelcomePage extends AdminLogin {
private JFrame frame;
private JButton btnAdmin;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WelcomePage window = new WelcomePage();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WelcomePage() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.BLUE);
frame.setBackground(Color.BLUE);
frame.setBounds(100, 100, 495, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblOnlineElectionManager = new JLabel(
"Welcome To Online Election Manager");
lblOnlineElectionManager.setForeground(Color.WHITE);
lblOnlineElectionManager.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblOnlineElectionManager.setBounds(100, 11, 284, 47);
frame.getContentPane().add(lblOnlineElectionManager);
JLabel lblLoginAs = new JLabel("Login as");
lblLoginAs.setForeground(Color.WHITE);
lblLoginAs.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblLoginAs.setBounds(195, 84, 77, 36);
frame.getContentPane().add(lblLoginAs);
btnAdmin = new JButton("ADMIN");
btnAdmin.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnAdmin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdmin) {
AdminLogin admin = new AdminLogin();
frame.setVisible(false);
admin.setVisible(true);
}
}
});
btnAdmin.setBounds(45, 192, 117, 36);
frame.getContentPane().add(btnAdmin);
JButton btnVoter = new JButton("VOTER");
btnVoter.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnVoter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnVoter.setBounds(267, 192, 117, 36);
frame.getContentPane().add(btnVoter);
}
}
我尝试过:
我尝试创建第二个类AdminLogin的实例,我想在单击此WelcomePage类上的按钮后移动到该实例。当我尝试运行时,在admin.setVisible(true)处出现错误。我做了什么拧?
What I have tried:
I tried creating an instance of the second class "AdminLogin" which i want to move to after clicking a button on this "WelcomePage" class. When i try to run there's an error at "admin.setVisible(true)". What did i do wring?
推荐答案
您是否在 AdminLogin中添加了 setVisible
方法
class?
Have you addedsetVisible
method in yourAdminLogin
class ?
public void setVisible(boolean b){
}
这篇关于单击java gui中的按钮后如何在框架/类之间移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文