如何仅在打印“Hello”的一个窗格上创建 JButton
How do I create a JButton solely on one pane that prints "Hello
每次我尝试这样做时,它都会给我一个错误,“Change Listener cannot be converted to Action Listener”,即使我对 class 实现了 ActionListener...它仍然给我另一个错误
有没有一种方法可以仅在“加密”面板上创建一个 JButton,当按下时打印“Hello”
这是我的代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwingDemo extends JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Encryption/Decryption Software");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
tabbedPane.addTab("Encryption", panel1);
tabbedPane.addTab("Decryption ", panel2);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200,170, 500,250);
frame.setVisible(true);
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(tabbedPane.getSelectedIndex() == 0){
panel1.removeAll();
panel1.setLayout(null);
JLabel initial_text = new JLabel("Enter text to be encrypted:");
JLabel final_text = new JLabel("Final text:");
JLabel key = new JLabel("Key:");
JTextField text_field = new JTextField(100);
JTextField key_field = new JTextField(100);
panel1.add(initial_text);
panel1.add(final_text);
panel1.add(key);
panel1.add(text_field);
panel1.add(key_field);
initial_text.setBounds(10, 20, 300, 50);
final_text.setBounds(10, 150, 600, 50);
key.setBounds(10, 58, 300, 50);
text_field.setBounds(178, 30, 230, 30);
key_field.setBounds(38, 72, 36, 25);
}
}
});
}
}
要检测点击,您需要添加 MouseListener 而非 ChangeListener
像这样
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class SwingDemo extends JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Encryption/Decryption Software");
JFrame frame = new JFrame("Encryption/Decryption Software");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
tabbedPane.addTab("Encryption", panel1);
tabbedPane.addTab("Decryption ", panel2);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 170, 500, 250);
frame.setVisible(true);
tabbedPane.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Hello");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
每次我尝试这样做时,它都会给我一个错误,“Change Listener cannot be converted to Action Listener”,即使我对 class 实现了 ActionListener...它仍然给我另一个错误
有没有一种方法可以仅在“加密”面板上创建一个 JButton,当按下时打印“Hello”
这是我的代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwingDemo extends JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Encryption/Decryption Software");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
tabbedPane.addTab("Encryption", panel1);
tabbedPane.addTab("Decryption ", panel2);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200,170, 500,250);
frame.setVisible(true);
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(tabbedPane.getSelectedIndex() == 0){
panel1.removeAll();
panel1.setLayout(null);
JLabel initial_text = new JLabel("Enter text to be encrypted:");
JLabel final_text = new JLabel("Final text:");
JLabel key = new JLabel("Key:");
JTextField text_field = new JTextField(100);
JTextField key_field = new JTextField(100);
panel1.add(initial_text);
panel1.add(final_text);
panel1.add(key);
panel1.add(text_field);
panel1.add(key_field);
initial_text.setBounds(10, 20, 300, 50);
final_text.setBounds(10, 150, 600, 50);
key.setBounds(10, 58, 300, 50);
text_field.setBounds(178, 30, 230, 30);
key_field.setBounds(38, 72, 36, 25);
}
}
});
}
}
要检测点击,您需要添加 MouseListener 而非 ChangeListener
像这样
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class SwingDemo extends JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Encryption/Decryption Software");
JFrame frame = new JFrame("Encryption/Decryption Software");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
tabbedPane.addTab("Encryption", panel1);
tabbedPane.addTab("Decryption ", panel2);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 170, 500, 250);
frame.setVisible(true);
tabbedPane.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Hello");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}