如何在一个窗格上创建一个仅包含“Hello”的JButton。

huangapple go评论85阅读模式
英文:

How do I create a JButton solely on one pane that prints "Hello

问题

import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;  // Import ActionEvent
import java.awt.event.ActionListener; // Import ActionListener

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);

                    // Add ActionListener to create JButton
                    JButton button = new JButton("Press Me");
                    panel1.add(button);
                    button.setBounds(10, 100, 100, 30);

                    button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("Hello");
                        }
                    });
                }
            }
        });
    }
}
英文:

Every time I try to do this, it gives me an error that "Change Listener cannot be converted to Action Listener" and even if I implement ActionListener to the class... it still gives me another error

Is there a way to create a JButton only on the pane "Encryption" that when pressed prints "Hello"

This is my code:

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);
}
}
});
}
}

答案1

得分: 0

要检测点击,您需要添加一个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");

        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) {
            }
        });
    }
}
英文:

to detect a click you need to add an MouseListener no a ChangeListener

Like this

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) {
}
});
}

huangapple
  • 本文由 发表于 2020年10月13日 21:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64336475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定