Java can';I don’我的画框上没有面板。。我做错了什么?
我一直有两个小窗口弹出。我没有看到来自Jpanel类的任何组件。我已经尽我所能。。我知道我做错了什么,但我似乎找不到错误 这是我的Jpanel课程Java can';I don’我的画框上没有面板。。我做错了什么?,java,swing,Java,Swing,我一直有两个小窗口弹出。我没有看到来自Jpanel类的任何组件。我已经尽我所能。。我知道我做错了什么,但我似乎找不到错误 这是我的Jpanel课程 public class ComponentsPanel extends JPanel { // variable declarations // constructor public ComponentsPanel() { panel = new
public class ComponentsPanel extends JPanel
{
// variable declarations
// constructor
public ComponentsPanel()
{
panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
pLabel = new javax.swing.JLabel("Policy #");
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(pLabel, gbc);
pTextField = new javax.swing.JTextField();
pTextField.setSize(10, 10);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(pTextField, gbc);
this.pNum = pTextField.getText();
newbLabel = new javax.swing.JLabel("NB Date");
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(newbLabel, gbc);
newbTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(newbTextField, gbc);
newbButton = new javax.swing.JButton("NEW DATE");
gbc.gridx = 2;
gbc.gridy = 1;
panel.add(newbButton, gbc);
this.newbDate = newbTextField.getText();
biLabel = new javax.swing.JLabel("BI Limits");
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(biLabel, gbc);
biTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(biTextField, gbc);
bilimButton = new javax.swing.JComboBox<>(bilimits);
bilimButton.setToolTipText("Choose Verified BILimits");
gbc.gridx = 2;
gbc.gridy = 2;
panel.add(bilimButton, gbc);
bicslButton = new javax.swing.JComboBox<>(bicsl);
gbc.gridx = 3;
gbc.gridy = 2;
panel.add(bicslButton, gbc);
this.biLimit = biTextField.getText();
lapseLabel = new javax.swing.JLabel("Lapse #");
gbc.gridx = 0;
gbc.gridy = 3;
panel.add(lapseLabel, gbc);
lapseTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 3;
panel.add(lapseTextField, gbc);
lapseButton = new javax.swing.JComboBox<>(lapse);
for (int i = 0; i < lapse.length; i++)
{
lapse[i] = Integer.toString(i);
if (i < 10)
lapse[i] = "0" + Integer.toString(i);
}
lapseButton.setModel(new DefaultComboBoxModel(lapse));
gbc.gridx = 2;
gbc.gridy = 3;
panel.add(lapseButton, gbc);
this.lapses = lapseTextField.getText();
noChangeButton = new javax.swing.JButton("NO CHANGE");
gbc.gridx = 0;
gbc.gridy = 4;
panel.add(noChangeButton, gbc);
changeButton = new javax.swing.JButton("CHANGE");
gbc.gridx = 1;
gbc.gridy = 4;
panel.add(changeButton, gbc);
decButton = new javax.swing.JButton("DECREASE");
gbc.gridx = 2;
gbc.gridy = 4;
panel.add(decButton, gbc);
incButton = new javax.swing.JButton("INCREASE");
gbc.gridx = 3;
gbc.gridy = 4;
panel.add(incButton, gbc);
cpyButton = new javax.swing.JButton("COPY");
cpyButton.setToolTipText("copy comment");
gbc.gridx = 0;
gbc.gridy = 5;
panel.add(cpyButton, gbc);
clrButton = new javax.swing.JButton("CLEAR");
clrButton.setToolTipText("clear all fields");
gbc.gridx = 3;
gbc.gridy = 5;
panel.add(clrButton, gbc);
dispTextArea = new javax.swing.JTextArea(10,10);
dispTextArea.setEditable(true);
dispTextArea.setLineWrap(true);
dispTextArea.setColumns(20);
dispTextArea.setRows(5);
panel.add(dispTextArea);
gbc.gridx = 0;
gbc.gridy = 6;
gbc.weightx = 0.5;
gbc.gridwidth = 4;
gbc.anchor = GridBagConstraints.PAGE_END;
panel.add(dispTextArea,gbc);
// adding listeners to components
// registering all components with their respective listeners
CompHandler compHandler = new CompHandler();
pTextField.addActionListener(compHandler);
biTextField.addActionListener(compHandler);
newbTextField.addActionListener(compHandler);
bilimButton.addActionListener(compHandler);
bicslButton.addActionListener(compHandler);
noChangeButton.addActionListener(compHandler);
changeButton.addActionListener(compHandler);
decButton.addActionListener(compHandler);
incButton.addActionListener(compHandler);
decButton.addActionListener(compHandler);
cpyButton.addActionListener(compHandler);
clrButton.addActionListener(compHandler);
}
// class to handle text fields
private class CompHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {}
} // end component handler class
}
在
组件spanel
中创建JPanel
调用面板
的实例,但决不将其添加到任何内容中
除非您正在做一个更复杂的布局,否则您可以直接将组件添加到ComponentsPanel
本身
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyWindow extends JFrame
{
public MyWindow()
{
super("FNA");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ComponentsPanel pane = new ComponentsPanel();
add(pane);
setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
new MyWindow();
}
});
}
public class ComponentsPanel extends JPanel
{
private final JLabel pLabel;
private final String pNum;
private final String newbDate;
private final String biLimit;
private final String lapses;
// variable declarations
// constructor
public ComponentsPanel()
{
GridBagConstraints gbc = new GridBagConstraints();
pLabel = new javax.swing.JLabel("Policy #");
gbc.gridx = 0;
gbc.gridy = 0;
add(pLabel, gbc);
JTextField pTextField = new javax.swing.JTextField();
pTextField.setSize(10, 10);
gbc.gridx = 1;
gbc.gridy = 0;
add(pTextField, gbc);
this.pNum = pTextField.getText();
JLabel newbLabel = new javax.swing.JLabel("NB Date");
gbc.gridx = 0;
gbc.gridy = 1;
add(newbLabel, gbc);
JTextField newbTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 1;
add(newbTextField, gbc);
JButton newbButton = new javax.swing.JButton("NEW DATE");
gbc.gridx = 2;
gbc.gridy = 1;
add(newbButton, gbc);
this.newbDate = newbTextField.getText();
JLabel biLabel = new javax.swing.JLabel("BI Limits");
gbc.gridx = 0;
gbc.gridy = 2;
add(biLabel, gbc);
JTextField biTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 2;
add(biTextField, gbc);
JComboBox<Object> bilimButton = new javax.swing.JComboBox<>();
bilimButton.setToolTipText("Choose Verified BILimits");
gbc.gridx = 2;
gbc.gridy = 2;
add(bilimButton, gbc);
JComboBox<Object> bicslButton = new javax.swing.JComboBox<>();
gbc.gridx = 3;
gbc.gridy = 2;
add(bicslButton, gbc);
this.biLimit = biTextField.getText();
JLabel lapseLabel = new javax.swing.JLabel("Lapse #");
gbc.gridx = 0;
gbc.gridy = 3;
add(lapseLabel, gbc);
JTextField lapseTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 3;
add(lapseTextField, gbc);
JComboBox<Object> lapseButton = new javax.swing.JComboBox<>();
// for (int i = 0; i < lapse.length; i++)
// {
// lapse[i] = Integer.toString(i);
// if (i < 10)
// {
// lapse[i] = "0" + Integer.toString(i);
// }
// }
// lapseButton.setModel(new DefaultComboBoxModel(lapse));
gbc.gridx = 2;
gbc.gridy = 3;
add(lapseButton, gbc);
this.lapses = lapseTextField.getText();
JButton noChangeButton = new javax.swing.JButton("NO CHANGE");
gbc.gridx = 0;
gbc.gridy = 4;
add(noChangeButton, gbc);
JButton changeButton = new javax.swing.JButton("CHANGE");
gbc.gridx = 1;
gbc.gridy = 4;
add(changeButton, gbc);
JButton decButton = new javax.swing.JButton("DECREASE");
gbc.gridx = 2;
gbc.gridy = 4;
add(decButton, gbc);
JButton incButton = new javax.swing.JButton("INCREASE");
gbc.gridx = 3;
gbc.gridy = 4;
add(incButton, gbc);
JButton cpyButton = new javax.swing.JButton("COPY");
cpyButton.setToolTipText("copy comment");
gbc.gridx = 0;
gbc.gridy = 5;
add(cpyButton, gbc);
JButton clrButton = new javax.swing.JButton("CLEAR");
clrButton.setToolTipText("clear all fields");
gbc.gridx = 3;
gbc.gridy = 5;
add(clrButton, gbc);
JTextArea dispTextArea = new javax.swing.JTextArea(10, 10);
dispTextArea.setEditable(true);
dispTextArea.setLineWrap(true);
dispTextArea.setColumns(20);
dispTextArea.setRows(5);
add(dispTextArea);
gbc.gridx = 0;
gbc.gridy = 6;
gbc.weightx = 0.5;
gbc.gridwidth = 4;
gbc.anchor = GridBagConstraints.PAGE_END;
add(dispTextArea, gbc);
// adding listeners to components
// registering all components with their respective listeners
CompHandler compHandler = new CompHandler();
pTextField.addActionListener(compHandler);
biTextField.addActionListener(compHandler);
newbTextField.addActionListener(compHandler);
bilimButton.addActionListener(compHandler);
bicslButton.addActionListener(compHandler);
noChangeButton.addActionListener(compHandler);
changeButton.addActionListener(compHandler);
decButton.addActionListener(compHandler);
incButton.addActionListener(compHandler);
decButton.addActionListener(compHandler);
cpyButton.addActionListener(compHandler);
clrButton.addActionListener(compHandler);
}
// class to handle text fields
private class CompHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {}
} // end component handler class
}
}
导入java.awt.EventQueue;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.DefaultComboxModel;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类MyWindow扩展了JFrame
{
公共MyWindow()
{
超级(“FNA”);
设置大小(300300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ComponentsPanel窗格=新的ComponentsPanel();
添加(窗格);
setVisible(真);
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
尝试
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex)
{
例如printStackTrace();
}
新建MyWindow();
}
});
}
公共类组件spanel扩展了JPanel
{
私人终审法院;
私有最终字符串pNum;
私人最终字符串日期;
私有最终字符串biLimit;
私人最终字符串失效;
//变量声明
//建造师
公共组件spanel()
{
GridBagConstraints gbc=新的GridBagConstraints();
pLabel=newjavax.swing.JLabel(“Policy#”);
gbc.gridx=0;
gbc.gridy=0;
添加(pLabel,gbc);
JTextField pTextField=newjavax.swing.JTextField();
pTextField.setSize(10,10);
gbc.gridx=1;
gbc.gridy=0;
添加(pTextField,gbc);
this.pNum=pTextField.getText();
JLabel newlabel=newjavax.swing.JLabel(“NB日期”);
gbc.gridx=0;
gbc.gridy=1;
添加(新标签,gbc);
JTextField newtextfield=newjavax.swing.JTextField();
gbc.gridx=1;
gbc.gridy=1;
添加(新文本字段,gbc);
jbuttonnewbutton=newjavax.swing.JButton(“新日期”);
gbc.gridx=2;
gbc.gridy=1;
添加(新按钮,gbc);
this.newbDate=newtextfield.getText();
JLabel biLabel=newjavax.swing.JLabel(“BI限制”);
gbc.gridx=0;
gbc.gridy=2;
添加(biLabel,gbc);
JTextField biTextField=newjavax.swing.JTextField();
gbc.gridx=1;
gbc.gridy=2;
添加(biTextField,gbc);
JComboBox-bilimButton=newjavax.swing.JComboBox();
设置工具脚本文本(“选择已验证的BILimits”);
gbc.gridx=2;
gbc.gridy=2;
添加(比林布顿,gbc);
JComboBox bicslButton=newjavax.swing.JComboBox();
gbc.gridx=3;
gbc.gridy=2;
添加(BICSL按钮,gbc);
this.biLimit=biTextField.getText();
JLabel-perseLabel=newjavax.swing.JLabel(“失效#”);
gbc.gridx=0;
gbc.gridy=3;
添加(标签,gbc);
JTextField=newjavax.swing.JTextField();
gbc.gridx=1;
gbc.gridy=3;
添加(文本字段,gbc);
JComboBox=newjavax.swing.JComboBox();
//for(int i=0;iimport java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyWindow extends JFrame
{
public MyWindow()
{
super("FNA");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ComponentsPanel pane = new ComponentsPanel();
add(pane);
setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
new MyWindow();
}
});
}
public class ComponentsPanel extends JPanel
{
private final JLabel pLabel;
private final String pNum;
private final String newbDate;
private final String biLimit;
private final String lapses;
// variable declarations
// constructor
public ComponentsPanel()
{
GridBagConstraints gbc = new GridBagConstraints();
pLabel = new javax.swing.JLabel("Policy #");
gbc.gridx = 0;
gbc.gridy = 0;
add(pLabel, gbc);
JTextField pTextField = new javax.swing.JTextField();
pTextField.setSize(10, 10);
gbc.gridx = 1;
gbc.gridy = 0;
add(pTextField, gbc);
this.pNum = pTextField.getText();
JLabel newbLabel = new javax.swing.JLabel("NB Date");
gbc.gridx = 0;
gbc.gridy = 1;
add(newbLabel, gbc);
JTextField newbTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 1;
add(newbTextField, gbc);
JButton newbButton = new javax.swing.JButton("NEW DATE");
gbc.gridx = 2;
gbc.gridy = 1;
add(newbButton, gbc);
this.newbDate = newbTextField.getText();
JLabel biLabel = new javax.swing.JLabel("BI Limits");
gbc.gridx = 0;
gbc.gridy = 2;
add(biLabel, gbc);
JTextField biTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 2;
add(biTextField, gbc);
JComboBox<Object> bilimButton = new javax.swing.JComboBox<>();
bilimButton.setToolTipText("Choose Verified BILimits");
gbc.gridx = 2;
gbc.gridy = 2;
add(bilimButton, gbc);
JComboBox<Object> bicslButton = new javax.swing.JComboBox<>();
gbc.gridx = 3;
gbc.gridy = 2;
add(bicslButton, gbc);
this.biLimit = biTextField.getText();
JLabel lapseLabel = new javax.swing.JLabel("Lapse #");
gbc.gridx = 0;
gbc.gridy = 3;
add(lapseLabel, gbc);
JTextField lapseTextField = new javax.swing.JTextField();
gbc.gridx = 1;
gbc.gridy = 3;
add(lapseTextField, gbc);
JComboBox<Object> lapseButton = new javax.swing.JComboBox<>();
// for (int i = 0; i < lapse.length; i++)
// {
// lapse[i] = Integer.toString(i);
// if (i < 10)
// {
// lapse[i] = "0" + Integer.toString(i);
// }
// }
// lapseButton.setModel(new DefaultComboBoxModel(lapse));
gbc.gridx = 2;
gbc.gridy = 3;
add(lapseButton, gbc);
this.lapses = lapseTextField.getText();
JButton noChangeButton = new javax.swing.JButton("NO CHANGE");
gbc.gridx = 0;
gbc.gridy = 4;
add(noChangeButton, gbc);
JButton changeButton = new javax.swing.JButton("CHANGE");
gbc.gridx = 1;
gbc.gridy = 4;
add(changeButton, gbc);
JButton decButton = new javax.swing.JButton("DECREASE");
gbc.gridx = 2;
gbc.gridy = 4;
add(decButton, gbc);
JButton incButton = new javax.swing.JButton("INCREASE");
gbc.gridx = 3;
gbc.gridy = 4;
add(incButton, gbc);
JButton cpyButton = new javax.swing.JButton("COPY");
cpyButton.setToolTipText("copy comment");
gbc.gridx = 0;
gbc.gridy = 5;
add(cpyButton, gbc);
JButton clrButton = new javax.swing.JButton("CLEAR");
clrButton.setToolTipText("clear all fields");
gbc.gridx = 3;
gbc.gridy = 5;
add(clrButton, gbc);
JTextArea dispTextArea = new javax.swing.JTextArea(10, 10);
dispTextArea.setEditable(true);
dispTextArea.setLineWrap(true);
dispTextArea.setColumns(20);
dispTextArea.setRows(5);
add(dispTextArea);
gbc.gridx = 0;
gbc.gridy = 6;
gbc.weightx = 0.5;
gbc.gridwidth = 4;
gbc.anchor = GridBagConstraints.PAGE_END;
add(dispTextArea, gbc);
// adding listeners to components
// registering all components with their respective listeners
CompHandler compHandler = new CompHandler();
pTextField.addActionListener(compHandler);
biTextField.addActionListener(compHandler);
newbTextField.addActionListener(compHandler);
bilimButton.addActionListener(compHandler);
bicslButton.addActionListener(compHandler);
noChangeButton.addActionListener(compHandler);
changeButton.addActionListener(compHandler);
decButton.addActionListener(compHandler);
incButton.addActionListener(compHandler);
decButton.addActionListener(compHandler);
cpyButton.addActionListener(compHandler);
clrButton.addActionListener(compHandler);
}
// class to handle text fields
private class CompHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {}
} // end component handler class
}
}