私は現在、大学でソフトウェア エンジニアリングを学んでおり、JPanel を JFrame に適切に追加する方法を理解するのに苦労しています。私のJPanelにはいくつかのボタンと、ボタンの1つをクリックしてActionListenerを使用することで変化するJLabelがあります。
私はそれを行うにはいくつかの方法があることを知っています.
私は負荷が間違っていることを知っていますが、それは何ですか?
これが私のコードです:
UIPanelOne:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
public class UIPanelOne extends JPanel implements ActionListener {
private JButton yes, no, cancel;
private JPanel panel;
private JLabel l1, l2;
UIPanelOne() {
    super();
    setVisible(true);
    //label dec
    panel = new JPanel();
    //buttons
    yes = new JButton("Yes");
    no = new JButton("No");
    cancel = new JButton("Cancel");
    //label dec
    l1 = new JLabel("Hello");
    //button dec
    panel.setLayout(new BorderLayout());
    panel.add(yes);
    panel.add(no);
    panel.add(cancel);
    panel.add(l1);
}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == yes) {
        l1.setText("OK then!");
    }else if (e.getSource() == no){
        l1.setText("Goodbye then!");
    }else if(e.getSource() == cancel){
        System.exit(0);
    }
}
public JComponent getGUI(){
    return panel;
}
}
UIフレーム:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UIFrame extends JFrame{
//constructor
public UIFrame(){
    //layout
    super("Can I help you?");
    setSize(400,600);
    setLayout(new BorderLayout());
    setVisible(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    UIPanelOne hello = new UIPanelOne();
    getContentPane().add(hello.getGUI());
   }
}
UIテスト:
public class UITest {
public static void main(String[] args){
UIFrame frame = new UIFrame();
frame.pack();
}
}
私はこれの多くがおそらく間違っていることを知っており、私は完全なアマチュアですが、助けを借りて改善したいと思っています!