Re: Newbie: What's wrong with my code?
From: Sebastian Scheid (sebastian_scheid_at_web.de)
Date: 05/21/04
- Next message: sam: "Re: how to write a thin client using java to access a remote system"
- Previous message: T.J. Willis: "Re: Metal theme in an applet."
- In reply to: Mike: "Newbie: What's wrong with my code?"
- Next in thread: Roedy Green: "Re: Newbie: What's wrong with my code?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 21 May 2004 03:15:53 +0200
"Mike" <mike.bosschaert@hccnet.nl> schrieb im Newsbeitrag
news:619df627.0405201411.3174ab98@posting.google.com...
> I try to create a JTabbedPane object, but it does not work. What is
wrong??
> Mike
>
> ======Codelist.java========
> import java.awt.*;
> import javax.swing.*;
>
> public class Codelist extends JPanel {
> public JTabbedPane Codelist(String var) {
here is your problem. This is not the constructor. A constructor does not
return any value.
public Codelist(String var) {} is what you wanted to do, right?
> JTabbedPane pn = new JTabbedPane();
> JPanel p1 = new JPanel(new GridBagLayout());
> p1.add(new JButton(var));
> pn.addTab("tab1",p1);
> return pn;
> }
> public static void main(String[] args){
> final JFrame frame = new JFrame("test");
> frame.addWindowListener(new WindowAdapter()
> {public void windowClosing(WindowEvent e) {System.exit(0);}});
> JPanel p = new JPanel(new GridBagLayout());
> Codelist c = new Codelist("but1");
you call the default constructor here. On c you could invoke
Codelist("but1") and get what you want. But that is NOT the way you should
do such a thing! See below...
> p.add(c);
> frame.getContentPane().add(p);
> frame.pack();
> frame.show();
> }
> }
Try:
import everything.you.need;
public class TestCodeList {
// do not subclass Swing components for a work that this method can do
private static createCodeList(String var) {
JTabbedPane pn = new JTabbedPane();
JPanel p1 = new JPanel(new GridBagLayout());
p1.add(new JButton(var));
pn.addTab("tab1",p1);
return pn;
}
public static void main(String[] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel p = new JPanel(new GridBagLayout());
JTabbedPane c = createCodeList("but1");
p.add(c);
frame.getContentPane().add(p);
frame.pack();
frame.show();
}
}
Sebastian
- Next message: sam: "Re: how to write a thin client using java to access a remote system"
- Previous message: T.J. Willis: "Re: Metal theme in an applet."
- In reply to: Mike: "Newbie: What's wrong with my code?"
- Next in thread: Roedy Green: "Re: Newbie: What's wrong with my code?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|