javaでアプリケーション作成中です。
ページ遷移をしたいのですが、frameを消してnewをして~を繰り返しては移動させたframeが初期の位置に戻ってしまいます。
なのでpanelに部品を埋め込み、遷移するさいにはpanelをremoveAllして新規panelをnewしたいと思ったのですが、
panelを消せずに詰まっています。
どうか改善点・または別の案を押してください。
java
1 2import java.awt.BorderLayout; 3import java.awt.Container; 4import java.awt.event.ActionEvent; 5 6import javax.swing.JButton; 7import javax.swing.JFrame; 8import javax.swing.JPanel; 9import javax.swing.JTextField; 10 11class SSample3_1 extends JFrame { 12 JButton btn1, btn2, btn3, btn4; 13 JPanel p; 14 SSample3_1 dodai2; 15 16 public static void main(String args[]) { 17 SSample3_1 frame = new SSample3_1("タイトル"); 18 frame.setVisible(true); 19 } 20 21 SSample3_1(String title) { 22 setTitle(title); 23 setBounds(100, 100, 300, 250); 24 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 25 26 p = new JPanel(); 27 28 btn1 = new JButton("Button"); 29 btn2 = new JButton("Button"); 30 btn3 = new JButton("Button"); 31 32 p.add(btn1); 33 p.add(btn2); 34 p.add(btn3); 35 36 btn1.addActionListener(null); 37 btn2.addActionListener(null); 38 btn3.addActionListener(null); 39 40 Container contentPane = getContentPane(); 41 contentPane.add(p, BorderLayout.CENTER); 42 } 43 private void junbi2() { 44 45 JTextField text1 = new JTextField(); 46 text1.setText("aaaaa"); 47 48 p.add(text1); 49 setDefaultCloseOperation(EXIT_ON_CLOSE); 50 51 } 52 53 public void actionPerformed(ActionEvent e) { 54 if (e.getSource() == btn1) { 55 p.removeAll(); 56 dodai2 = new SSample3_1("title"); 57 this.junbi2(); 58 dodai2.setVisible(true); 59 } else if (e.getSource() == btn2) { 60 p.removeAll(); 61 } else if (e.getSource() == btn3) { 62 p.removeAll(); 63 } 64 } 65}
removeAllです。失礼しました。
イベントを追加についてですが、
btn1.addActionListener(null)
をthisに変更するとEclipseではエラーになってしまいます。
イベントはゆくゆくは別のファイルに記述したいと考えておりますが、
現状は汚くても実装させることを先に考えています。
btn1...にイベントをaddできてもいません。
(A) インターフェースActionListenerを実装したクラスのインスタンス
java
1package db_test; 2 3import java.awt.BorderLayout; 4import java.awt.Container; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7 8import javax.swing.JButton; 9import javax.swing.JFrame; 10import javax.swing.JPanel; 11import javax.swing.JTextField; 12 13class SSample3_1 extends JFrame implements ActionListener { 14 JButton btn1, btn2, btn3, btn4; 15 JPanel p; 16// SSample3_1 dodai2; 17 18 public static void main(String args[]) { 19 SSample3_1 frame = new SSample3_1("タイトル"); 20 frame.setVisible(true); 21 } 22 23 SSample3_1(String title) { 24 setTitle(title); 25 setBounds(100, 100, 300, 250); 26 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27 28 p = new JPanel(); 29 30 btn1 = new JButton("Button"); 31 btn2 = new JButton("Button"); 32 btn3 = new JButton("Button"); 33 34 p.add(btn1); 35 p.add(btn2); 36 p.add(btn3); 37 38 btn1.addActionListener(this); 39 btn2.addActionListener(this); 40 btn3.addActionListener(this); 41 42 Container contentPane = getContentPane(); 43 contentPane.add(p, BorderLayout.CENTER); 44 } 45 46 private void junbi2() { 47 48 JTextField text1 = new JTextField(); 49 text1.setText("aaaaa"); 50 51 p.add(text1); 52 setDefaultCloseOperation(EXIT_ON_CLOSE); 53 54 } 55 56 public void actionPerformed(ActionEvent e) { 57 if (e.getSource() == btn1) { 58 this.p.removeAll(); 59 // dodai2 = new SSample3_1("title"); 60 // this.junbi2(); 61 // dodai2.setVisible(true); 62 } else if (e.getSource() == btn2) { 63 p.removeAll(); 64 } else if (e.getSource() == btn3) { 65 p.removeAll(); 66 } 67 } 68}
回答1件
あなたの回答
tips
プレビュー