【发布时间】:2014-03-30 17:32:11
【问题描述】:
这是我第一次遇到堆栈溢出问题,我真的发现它对我遇到的几乎所有问题都有帮助。我终于只需要获得一个帐户,因为我终于有一个我找不到答案的问题。我现在使用的是 Netbeans 7.3,并且是第一次使用 GUI 编辑器。我需要精确定位,所以我无法手动执行 GUI。我的问题是我有一个 JFrame 表单,其中添加了一个 JPanel 表单(我只是将它放在窗口上,它似乎工作正常),我也想添加第二个 JPanel,但无论我是否这样做基于第一个 JPanel 中的 jcombobox 选择。我首先在第一个 JFrame 中的 jcombobox 中添加了一个动作侦听器。我的计划是从与该 jcombobox 相关联的操作执行方法中添加第二个 JPanel,但我不知道如何添加它。如果从那以后我没有赚很多钱,我很抱歉。我是初学者。同样通过阅读我的代码,您可能会发现我正在尝试做什么,但老实说,在我弄清楚 gui 之前我什至无法到达那里。也许一些代码可能会有所帮助:
这是 JFrame 代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package geometry.program;
import javax.swing.JPanel;
/**
*
* @author maxwellparker
*/
public class Main extends javax.swing.JFrame {
/**
* Creates new form Main
*/
public Main() {
super("Geometry Sucks");
initComponents();
this.setLocationRelativeTo(null);
}
public void addPanel(JPanel j){
//i was going to call this method from the first jpanel on this JFrame already to add the second one.
}
/**
* 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() {
rP1 = new geometry.program.RP();
control1 = new geometry.program.Control();
rP2 = new geometry.program.RP();
rP3 = new geometry.program.RP();
control2 = new geometry.program.Control();
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(layout.createSequentialGroup()
.addContainerGap()
.add(control2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(92, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(control2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(189, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private geometry.program.Control control1;
private geometry.program.Control control2;
private geometry.program.RP rP1;
private geometry.program.RP rP2;
private geometry.program.RP rP3;
// End of variables declaration
}
这是 JPanel 1 代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package geometry.program;
/**
*
* @author maxwellparker
*/
public class Control extends javax.swing.JPanel {
/**
* Creates new form Control
*/
public Control() {
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() {
Mode = new javax.swing.JLabel();
ModeComboBox = new javax.swing.JComboBox();
Var = new javax.swing.JLabel();
VarField = new javax.swing.JTextField();
Val = new javax.swing.JLabel();
ValField = new javax.swing.JTextField();
CustomVar = new javax.swing.JLabel();
Mode.setText("Mode:");
ModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Regular Polygons", "Second", "Third", "Fourth" }));
ModeComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ModeComboBoxActionPerformed(evt);
}
});
Var.setText("Variable:");
Val.setText("Value:");
CustomVar.setText("Custom Variable (Optional):");
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(CustomVar)
.add(layout.createSequentialGroup()
.add(Var)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(VarField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(Val)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(ValField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(Mode)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(ModeComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(Mode)
.add(ModeComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(CustomVar)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(Var)
.add(Val)
.add(VarField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(ValField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
private void ModeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
if(ModeComboBox.getSelectedIndex()==0){
//run option 1
System.out.println("Add corresponding option Panel To JFrame.");
}else if(ModeComboBox.getSelectedIndex()==1){
System.out.println("Add corresponding option Panel To JFrame.");
}else if(ModeComboBox.getSelectedIndex()==2){
System.out.println("Add corresponding option Panel To JFrame.");
}else if(ModeComboBox.getSelectedIndex()==3){
System.out.println("Add corresponding option Panel To JFrame.");
}
}
// Variables declaration - do not modify
private javax.swing.JLabel CustomVar;
private javax.swing.JLabel Mode;
private javax.swing.JComboBox ModeComboBox;
private javax.swing.JLabel Val;
private javax.swing.JTextField ValField;
private javax.swing.JLabel Var;
private javax.swing.JTextField VarField;
// End of variables declaration
}
这是第二个 JPanel 代码(我想根据 JPanel 1 中的组合框添加到 JFrame 中的代码)。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package geometry.program;
/**
*
* @author maxwellparker
*/
public class Control extends javax.swing.JPanel {
/**
* Creates new form Control
*/
public Control() {
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() {
Mode = new javax.swing.JLabel();
ModeComboBox = new javax.swing.JComboBox();
Var = new javax.swing.JLabel();
VarField = new javax.swing.JTextField();
Val = new javax.swing.JLabel();
ValField = new javax.swing.JTextField();
CustomVar = new javax.swing.JLabel();
Mode.setText("Mode:");
ModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Regular Polygons", "Second", "Third", "Fourth" }));
ModeComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ModeComboBoxActionPerformed(evt);
}
});
Var.setText("Variable:");
Val.setText("Value:");
CustomVar.setText("Custom Variable (Optional):");
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(CustomVar)
.add(layout.createSequentialGroup()
.add(Var)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(VarField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(Val)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(ValField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(Mode)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(ModeComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(Mode)
.add(ModeComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(CustomVar)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(Var)
.add(Val)
.add(VarField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(ValField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
private void ModeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
if(ModeComboBox.getSelectedIndex()==0){
//run option 1
System.out.println("Add corresponding option Panel To JFrame.");
}else if(ModeComboBox.getSelectedIndex()==1){
System.out.println("Add corresponding option Panel To JFrame.");
}else if(ModeComboBox.getSelectedIndex()==2){
System.out.println("Add corresponding option Panel To JFrame.");
}else if(ModeComboBox.getSelectedIndex()==3){
System.out.println("Add corresponding option Panel To JFrame.");
}
}
// Variables declaration - do not modify
private javax.swing.JLabel CustomVar;
private javax.swing.JLabel Mode;
private javax.swing.JComboBox ModeComboBox;
private javax.swing.JLabel Val;
private javax.swing.JTextField ValField;
private javax.swing.JLabel Var;
private javax.swing.JTextField VarField;
// End of variables declaration
}
非常感谢您的帮助。
【问题讨论】:
-
这个问题对我来说真的很难理解。
标签: java swing user-interface netbeans jcombobox