import java.applet.Applet;import java.awt.*;
import javax.swing.*;public class SimpleTab extends JFrame{
JTabbedPane jtp;
String[] pages ={" Rule1"," Rule2"," Rule3"," Rule4"," Rule5"," Rule6"}; public SimpleTab() {
super("General Decoder Manager");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contents = getContentPane();
jtp = new JTabbedPane();
JPanel pnlRule = new JPanel();
JPanel pnlConfig = new JPanel();
JPanel pnlRuleright = new JPanel();
JList RuleList = new JList(pages);
RuleList.setPreferredSize(new Dimension(150,100));
RuleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pnlRule.setLayout(new BorderLayout());
pnlRule.add(new JScrollPane(RuleList),BorderLayout.WEST);
pnlRuleright.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel lblKeyWord = new JLabel("Key Word");
JTextField edtKeyWord = new JTextField(20);
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//((GridBagLayout)getLayout()).setConstraints(lblKeyWord,c); //这里报错
pnlRule.add(lblKeyWord);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//((GridBagLayout)getLayout()).setConstraints(edtKeyWord,c); //这里报错
pnlRule.add(edtKeyWord);
pnlRule.add(pnlRuleright,BorderLayout.CENTER);
jtp.addTab("Rule Manager",pnlRule);
jtp.addTab("GDM configure",pnlConfig);
contents.add(jtp);
setVisible(true); } public static void main(String[] args) {
SimpleTab simpleTab = new SimpleTab();
}}
报错信息为:
Exception in thread "main" java.lang.ClassCastException: java.awt.BorderLayout
at SimpleTab.<init>(SimpleTab.java:42)
at SimpleTab.main(SimpleTab.java:55)
Process exited with exit code 1.大家帮忙指点,谢谢
import javax.swing.*;public class SimpleTab extends JFrame{
JTabbedPane jtp;
String[] pages ={" Rule1"," Rule2"," Rule3"," Rule4"," Rule5"," Rule6"}; public SimpleTab() {
super("General Decoder Manager");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contents = getContentPane();
jtp = new JTabbedPane();
JPanel pnlRule = new JPanel();
JPanel pnlConfig = new JPanel();
JPanel pnlRuleright = new JPanel();
JList RuleList = new JList(pages);
RuleList.setPreferredSize(new Dimension(150,100));
RuleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pnlRule.setLayout(new BorderLayout());
pnlRule.add(new JScrollPane(RuleList),BorderLayout.WEST);
pnlRuleright.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel lblKeyWord = new JLabel("Key Word");
JTextField edtKeyWord = new JTextField(20);
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//((GridBagLayout)getLayout()).setConstraints(lblKeyWord,c); //这里报错
pnlRule.add(lblKeyWord);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//((GridBagLayout)getLayout()).setConstraints(edtKeyWord,c); //这里报错
pnlRule.add(edtKeyWord);
pnlRule.add(pnlRuleright,BorderLayout.CENTER);
jtp.addTab("Rule Manager",pnlRule);
jtp.addTab("GDM configure",pnlConfig);
contents.add(jtp);
setVisible(true); } public static void main(String[] args) {
SimpleTab simpleTab = new SimpleTab();
}}
报错信息为:
Exception in thread "main" java.lang.ClassCastException: java.awt.BorderLayout
at SimpleTab.<init>(SimpleTab.java:42)
at SimpleTab.main(SimpleTab.java:55)
Process exited with exit code 1.大家帮忙指点,谢谢
((GridBagLayout) pnlRuleright.getLayout()).setConstraints(lblKeyWord,c);
前面要加对象,晕用JAVA做GUI,快死人了
---------------------
getLayout()前面没有对象,那就是缺省对象this,即相当于
this.getLayout();//this就是SimpleTab的isntance
显然,你的GridBagLayout是设置到pnlRuleright的,而不是设置到this,所以this.getLayout();返回的对象就不是GridBagLayout的实例,所以强行转换就会出错。