如何让 JButton 打开不同的框架?
How do I make JButton open different frames?
我创建了 2 个 JButton,但都打开了同一个文件 我如何让第二个按钮打开另一个文件...................................... ..................................................... ..................................................... .....................
//Starting Page
import javax.swing.*;
import java.awt.event.*;
import java.awt. *;
import java.io.*;
public class AddressBook implements ActionListener // Create a new class Address Book
{
JFrame Start=new JFrame("Address Book"); // Set name of Frame
JButton Open; // Set new button
JButton Second;
{
Open=new JButton("OPEN"); // set name of button
Second=new JButton("Second");
Start.setSize(500,600); // set size of frame
Start.add(new JLabel(new ImageIcon("backgroundforlab.jpg"))); // add background picture
Start.setVisible(true);
Start.setLayout(null);
Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Start.getContentPane().add(Open); //Make button visible
Start.getContentPane().add(Second);
Open.setBounds(100,385,295,88);
Second.setBounds(50,160,150,44); // set size of button
Open.addActionListener(this);
Second.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Start.dispose(); // When button is clicked close frame and open mainMenu
mainMenu A=new mainMenu();
}
public static void main(String ag[])
{
AddressBook A=new AddressBook(); // run class AddressBook
}
}
你可以...
为您的按钮使用单独的 ActionListener
s
Open.addActionListener(new OpenActionListener());
Second.addActionListener(new SecondActionListener());
您需要提供 ActionListener
的实现作为额外的 classes
这可能是首选方法之一,因为它将操作的 functionality/responsibility 隔离为单个 class,但它确实创建了一堆小的 class.
你可以..
改用匿名 classes...
Open.addActionListener(new ActionListener() {
@Overrride
public void actionPerformed(ActionEvent e) {
//...
}
});
这与之前的思路基本相同,只是不需要单独的class来管理
你可以...
使用actionCommand
属性来识别按钮
Open.setActionCommand("open");
Second.setActionCommand("second");
//...
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("open".equals(command)) {
//...
} else if ("second".equals(command)) {
//...
}
}
如果您有许多重复操作的按钮(如菜单和工具栏按钮),这很好
你可以...
使用source
属性来识别按钮
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == open) {
//...
} else if (source == second) {
//...
}
}
这仅在 ActionListener
可以访问按钮的实际引用时才有效。这使得它的使用受到限制,特别是因为有更好的解决方案可用
您还应该看看:
了解更多详情和想法
我创建了 2 个 JButton,但都打开了同一个文件 我如何让第二个按钮打开另一个文件...................................... ..................................................... ..................................................... .....................
//Starting Page
import javax.swing.*;
import java.awt.event.*;
import java.awt. *;
import java.io.*;
public class AddressBook implements ActionListener // Create a new class Address Book
{
JFrame Start=new JFrame("Address Book"); // Set name of Frame
JButton Open; // Set new button
JButton Second;
{
Open=new JButton("OPEN"); // set name of button
Second=new JButton("Second");
Start.setSize(500,600); // set size of frame
Start.add(new JLabel(new ImageIcon("backgroundforlab.jpg"))); // add background picture
Start.setVisible(true);
Start.setLayout(null);
Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Start.getContentPane().add(Open); //Make button visible
Start.getContentPane().add(Second);
Open.setBounds(100,385,295,88);
Second.setBounds(50,160,150,44); // set size of button
Open.addActionListener(this);
Second.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Start.dispose(); // When button is clicked close frame and open mainMenu
mainMenu A=new mainMenu();
}
public static void main(String ag[])
{
AddressBook A=new AddressBook(); // run class AddressBook
}
}
你可以...
为您的按钮使用单独的 ActionListener
s
Open.addActionListener(new OpenActionListener());
Second.addActionListener(new SecondActionListener());
您需要提供 ActionListener
的实现作为额外的 classes
这可能是首选方法之一,因为它将操作的 functionality/responsibility 隔离为单个 class,但它确实创建了一堆小的 class.
你可以..
改用匿名 classes...
Open.addActionListener(new ActionListener() {
@Overrride
public void actionPerformed(ActionEvent e) {
//...
}
});
这与之前的思路基本相同,只是不需要单独的class来管理
你可以...
使用actionCommand
属性来识别按钮
Open.setActionCommand("open");
Second.setActionCommand("second");
//...
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("open".equals(command)) {
//...
} else if ("second".equals(command)) {
//...
}
}
如果您有许多重复操作的按钮(如菜单和工具栏按钮),这很好
你可以...
使用source
属性来识别按钮
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == open) {
//...
} else if (source == second) {
//...
}
}
这仅在 ActionListener
可以访问按钮的实际引用时才有效。这使得它的使用受到限制,特别是因为有更好的解决方案可用
您还应该看看:
了解更多详情和想法