Mam problem ze zrobieniem aplikacji okienkowej. Chciałem zrobić coś takiego, żeby przyciśnięcie przycisku "Open" powodowało zmiane zawartości JFrame i wyświetliło nowy panel. Niestety - przyciśnięcie przycisku Open otwiera mi nowego JFrame z zawartością nowego Panelu. Jak to zmienic?
Tu są obrazki jak wyglada aplikacja.
Chciałbym przejść z tego panelu za pomocą przycisku Open...
http://img35.imageshack.us/my.php?image=23551975.jpg
do tego...
http://img33.imageshack.us/my.php?image=17028630.jpg
Kod, który napisałem ma postać:
- JFrame.java
public class JFrame extends javax.swing.JFrame {
/** Creates new form JFrame */
public JFrame() {
initComponents();
setContentPane(new Panel());
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
}
- Panel.java
public class Panel extends javax.swing.JPanel {
/** Creates new form Panel */
public Panel() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
OpenB = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
OpenB.setText("Open");
OpenB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenBActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(159, 159, 159)
.add(OpenB))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(88, Short.MAX_VALUE)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(18, 18, 18)
.add(OpenB)
.addContainerGap())
);
}// </editor-fold>
private void OpenBActionPerformed(java.awt.event.ActionEvent evt) {
JFrame jf = new JFrame();
jf.setContentPane(new Panel2());
jf.setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton OpenB;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
- Panel2.java
public class Panel2 extends javax.swing.JPanel {
/** Creates new form Panel2 */
public Panel2() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
CloseB = new javax.swing.JButton();
CloseB.setText("CloseB");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(153, 153, 153)
.add(CloseB)
.addContainerGap(182, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(251, Short.MAX_VALUE)
.add(CloseB)
.add(26, 26, 26))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton CloseB;
// End of variables declaration
}