英文:
JFrame: navigate to another frame using JButton
问题
我在Java Swing中尝试使用按钮进行导航。
当我点击按钮时,我想转到另一个现有的窗口。
我首先会附上菜单面板:
public class MenuPanel extends JFrame implements ActionListener {
JLabel l1,l2;
JButton btn1,btn2,btn3,btn4;
MenuPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("菜单面板 - 最终项目");
l1 = new JLabel("菜单面板示例:");
l2 = new JLabel("此程序由 Ghetto K 制作");
l1.setForeground(Color.RED);
l1.setFont(new Font("Arial", Font.BOLD, 28));
l2.setForeground(Color.RED);
l2.setFont(new Font("Arial", Font.ITALIC,22));
btn1 = new JButton("添加子项");
btn2 = new JButton("添加工作者");
btn3 = new JButton("添加班级");
btn4 = new JButton("添加任务");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
l1.setBounds(150, 30, 400, 30);
l2.setBounds(150, 80, 400, 30);
btn1.setBounds(0, 230, 120, 30);
btn2.setBounds(160, 230, 120, 30);
btn3.setBounds(465, 230, 120, 30);
btn4.setBounds(320, 230, 120, 30);
add(l1);
add(l2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
public static void main(String[] args) {
new MenuPanel();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
}
else if(e.getSource()==btn2)
{
}
else if(e.getSource()==btn3)
{
}
}
}
我想导航到这个面板:
public class AddWorkerPanel extends JFrame implements ActionListener {
static Connection con = DatabaseConnection.getConnection();
JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton btn1, btn2;
AddWorkerPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Java中的添加工作者表单");
l1 = new JLabel("添加工作者表单 - 在下方输入详细信息:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("工作者姓名:");
l3 = new JLabel("工作者班级编号:");
tf1 = new JTextField();
tf2 = new JTextField();
btn1 = new JButton("提交");
btn2 = new JButton("清除");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
btn1.setBounds(80, 230, 200, 30);
btn2.setBounds(300, 230, 200, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(btn1);
add(btn2);
}
public static void main(String[] args) {
new AddWorkerPanel();
}
}
英文:
I'm trying to navigate with buttons in Java Swing.
I want to go to another existing frame when i click on a button.
I will attach first the Menu Panel:
public class MenuPanel extends JFrame implements ActionListener {
JLabel l1,l2;
JButton btn1,btn2,btn3,btn4;
MenuPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Menu Panel For Final Project");
l1 = new JLabel("Menu Panel Bitches: ");
l2 = new JLabel("This Program Made By Ghetto K");
l1.setForeground(Color.RED);
l1.setFont(new Font("Arial", Font.BOLD, 28));
l2.setForeground(Color.RED);
l2.setFont(new Font("Arial", Font.ITALIC,22));
btn1 = new JButton("Add Child");
btn2 = new JButton("Add Worker");
btn3 = new JButton("Add Class");
btn4 = new JButton("Add Task");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
l1.setBounds(150, 30, 400, 30);
l2.setBounds(150, 80, 400, 30);
btn1.setBounds(0, 230, 120, 30);
btn2.setBounds(160, 230, 120, 30);
btn3.setBounds(465, 230, 120, 30);
btn4.setBounds(320, 230, 120, 30);
add(l1);
add(l2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
public static void main(String[] args) {
new MenuPanel();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
}
else if(e.getSource()==btn2)
{
}
else if(e.getSource()==btn3)
{
}
}
I want to navigate to this panel:
public class AddWorkerPanel extends JFrame implements ActionListener {
static Connection con = DatabaseConnection.getConnection();
JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton btn1, btn2;
AddWorkerPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Add Worker form in Java");
l1 = new JLabel("Add Worker Form - Type Details Below:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Worker-Name:");
l3 = new JLabel("Worker-Class-Number:");
tf1 = new JTextField();
tf2 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
btn1.setBounds(80, 230, 200, 30);
btn2.setBounds(300, 230, 200, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(btn1);
add(btn2);
}
public static void main(String[] args) {
new AddWorkerPanel();
}
}
答案1
得分: 1
JFrame
是实际显示在屏幕上的窗口框架。因此,您想要做的是从JPanel
派生出您的MenuPanel
和AddWorkerPanel
类,以便它们都可以放置在一个窗口框架内。然后,在您的主窗口类中,您可以有一个使用CardLayout
来容纳这两个面板,并且可以有一个按钮,通过使用存储的卡片布局实例中的next()
方法在卡片布局中循环显示卡片。
英文:
A JFrame
is the actual frame that sits on the screen. So what you want to do is derive your MenuPanel
and AddWorkerPanel
classes from JPanel
, so that they can both sit inside of one frame. Then, in your main frame class, you can have a CardLayout
holding both of your panels, and have a button that cycles through the cards on the card layout, using the next()
method in the card layout instance that you have stored.
答案2
得分: -1
如果您只想关闭一个JFrame,然后打开另一个JFrame,您只需要在按钮的调用方法中简单地执行以下操作:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AddWorkerPanel();
super.dispose();
}
});
英文:
If you just want to close one JFrame and then open the other you should be avble by just simply doing this in the calling method of your button:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AddWorkerPanel();
super.dispose();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论