JAVA JTextField doesnt show up - java

So Im trying to make a little program to calculate the area of a specific shape.
The user should be able to make a input via a textfield (Like the height and stuff of the shapes). The he should press a button and the price should get printed.
But it doesnt show up.
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rechner extends JFrame implements ActionListener{
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JButton button4;
private static JTextField numberField;
private JPanel jpanel;
public Rechner(String titel){
super(titel);
jpanel = new JPanel();
numberField = new JTextField(1500);
add(numberField, BorderLayout.CENTER);
button1 = new JButton("Rechteck");
button1.setBounds(10, 10, 150, 30);
button1.addActionListener(this);
add(button1);
button2 = new JButton("Dreieck");
button2.setBounds(170, 10, 150, 30);
button2.addActionListener(this);
add(button2);
button3 = new JButton("Trapez");
button3.setBounds(330, 10, 150, 30);
button3.addActionListener(this);
add(button3);
button4 = new JButton("Parallelogramm");
button4.setBounds(490, 10, 150, 30);
button4.addActionListener(this);
add(button4);
setResizable(false);
}
public static void main(String[] args) {
Rechner frame = new Rechner("Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(660, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
System.out.println("fff");
}
String numberStr = numberField.getText();
}
}

The default layout manager of a JFrame (well, of its content pane in fact) is BorderLayout.
Without specified constraints , the component is added to BorderLayout.CENTER, so
add(component);
is the same as
add(component, BorderLayout.CENTER);
and each component added this way will replace the last component added to the center.
Also note that setBounds will have no effect if there is a layout manager, and that you create a JPanel that you never use.
Finally, you may want to have a look at this guide : A Visual Guide to Layout Managers

This line is mainly the problem:
add(numberField, BorderLayout.CENTER);
Is causing the TextField to fill the entire space. Then, the next time you add a component to the JFrame with BorderLayout.CENTER, the JTextField gets replaced. To fix this:
super(titel);
jpanel = new JPanel();
add(jpanel, BorderLayout.NORTH); //adding the jpanel
button1 = new JButton("Rechteck");
jpanel.add(button1);
button1.setBounds(10, 10, 150, 30);
//adding the other buttons to the JPanel...
//...
//...
button4.addActionListener(this);
button3.addActionListener(this);
button2.addActionListener(this);
button1.addActionListener(this);
numberField = new JTextField(1500);
add(numberField);//this will cause it to fill the remaining space
setResizable(false);
Explanation:
The buttons should go into the JPanel you created, and the JPanel should go into the JFrame's NORTH. That way they don't cover the JFrame

Related

How to add spacing between JPanel and JFrame's contentPane?

This is the picture I am trying to replicate
This is what I have (didn't add icon images yet)
I can't seem to find a solution, been staring at it for quite some time.
I am trying to replicate the following picture, using GridLayout for the buttons and the figure out the rest on my own using Java Swing. Furthermore, I've added my buttons into a JPanel and now I'm trying to add spacing between the panel and the pane.
This is what I have, how can I go about it?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
The easiest way to do it would be to add an empty border to your JPanel (see this post on empty borders):
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// ...
panel.setBorder(new EmptyBorder(50, 50, 50, 50));
Another good approach (depending always on your application needs), if you have the JButton preferred size set, would be to have the main JPanel's grid layout set to have two columns and one row, with another JPanel inside each column. Adding to the interior JPanels a BoxLayout in Y_AXIS mode and aligning the buttons with setAlignmentX() would work great too (note this approach wouldn't center the JButtons vertically) (see How to use BoxLayout):
public class MyFrame extends JFrame {
private String title = "Title";
public MyFrame(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,2,10,10));
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JButton b1 = new JButton();
b1.setBackground(Color.white);
//b1.setIcon(new ImageIcon(img1));
b1.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b1);
JButton b2 = new JButton();
b2.setBackground(Color.white);
//b2.setIcon(new ImageIcon(img2));
b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b2);
JButton b3 = new JButton();
b3.setBackground(Color.white);
//b3.setIcon(new ImageIcon(img3));
b3.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b3);
JButton b4 = new JButton();
b4.setBackground(Color.white);
//b4.setIcon(new ImageIcon(img4));
b4.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b4);
JButton b5 = new JButton();
b5.setBackground(Color.white);
//b5.setIcon(new ImageIcon(img5));
b5.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b5);
JButton b6 = new JButton();
b6.setBackground(Color.white);
//b6.setIcon(new ImageIcon(img6));
b6.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b6);
add(mainPanel); //Adding our mainPanel to the contentPane of the JFrame
this.setSize(500,500); //or pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
}
}
Here's a little demonstration I whipped up.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
You don't set the size of the JFrame and try and make the Swing components fit. You let the JFrame pack with all the Swing components.
You create a GridLayout JPanel inside of a FlowLayout JPanel. The FlowLayout JPanel uses an empty border of the appropriate size.
I used the image the OP provided to get the icons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EmptySpaceDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new EmptySpaceDemo());
}
private Image[] images;
public EmptySpaceDemo() {
this.images = createImages();
}
private Image[] createImages() {
BufferedImage image = readImage();
Image[] images = new Image[6];
images[0] = image.getSubimage(155, 113, 110, 90);
images[1] = image.getSubimage(276, 113, 110, 90);
images[2] = image.getSubimage(155, 217, 110, 90);
images[3] = image.getSubimage(276, 217, 110, 90);
images[4] = image.getSubimage(155, 321, 110, 90);
images[5] = image.getSubimage(276, 321, 110, 90);
return images;
}
#Override
public void run() {
JFrame frame = new JFrame("Empty Space Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createEmptyBorder(40, 100, 40, 100));
JPanel innerPanel = new JPanel(new GridLayout(0, 2, 10, 10));
innerPanel.setBackground(Color.BLACK);
for (int i = 0; i < images.length; i++) {
JButton button = new JButton(new ImageIcon(images[i]));
innerPanel.add(button);
}
panel.add(innerPanel);
return panel;
}
private BufferedImage readImage() {
try {
return ImageIO.read(getClass().getResourceAsStream("/icons.png"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

how to make jpanel in jframe visible

I try to write a menue for a little game in Java. I thought it would be a good idea to have a Window class (extending JFrame) and then put a JPanel in it for the different Screens (Menue, Game, GameOver etc)
If I put the buttons and stuff directly in the JFrame everything is shwown correct, but when I try to put a JPanel into the JFrame it doesn't work. Here is the code:
public class Window extends JFrame{
private final int WIDTH = 800;
private final int HEIGTH = 600;
private final int QUADRAT = 50;
JButton startButton;
JButton exitButton;
JButton anleitungButton;
JLabel gameTitle;
public Window() {
super("Study Run");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(WIDTH, HEIGTH);
setResizable(false);
getContentPane().add(new MenuePanel());
setVisible(true);
setLocationRelativeTo(null);
}
And this is my Panel:
public class MenuePanel extends JPanel{
JButton startButton;
JButton exitButton;
JButton anleitungButton;
JLabel gameTitle;
public MenuePanel() {
super();
setBackground(Color.CYAN);
gameTitle = new JLabel("StudyRun", SwingConstants.CENTER);
gameTitle.setBounds(200, 25, 400, 75);
gameTitle.setFont(new Font("Arial", Font.ITALIC, 36));
add(gameTitle);
startButton = new JButton("start");
startButton.setBounds(325, 125, 150, 50);
add(startButton);
anleitungButton = new JButton("anleitung");
anleitungButton.setBounds(325, 200, 150, 50);
add(anleitungButton);
exitButton = new JButton("exit");
exitButton.setBounds(325, 450, 150, 50);
add(exitButton);
CloseListener closeListener = new CloseListener();
StartListener startListener = new StartListener();
AnleitungListener anleitungListener = new AnleitungListener();
startButton.addActionListener(startListener);
anleitungButton.addActionListener(anleitungListener);
exitButton.addActionListener(closeListener);
}
The only help I found online was, that I needed to add the panel before I set the frame visible. That didn't work. Putting pack() or revalidate() anywhere in the code didn't work either. Also setting the Panel on opaque or visible didn't do anything. I don't know what else to try?!
Your problem is here:
setLayout(null);
When you use null layouts, you the coder are completely responsible for the location and size of all added components. Your added component has no size and so defaults to 0, 0.
A (bad) solution: give the MenuePanel a size or bounds
A much better solution: learn and use the layout managers (as all your searches most assuredly already told you).
It's best to remember that Java uses Flowlayout() as a default.
public Window() {
setLayout(new FlowLayout());
}
So your basically overwriting the layout to null as explained in the previous answer.Also if you plan to use different class and panels to add to a JFrame from different classes use a getter
class SomePanel{
public JComponent getPanel(){
return panel;
}
}
Then add to JFrame..
class MyFrame{
add(new SomePanel().getPanel());
}

Disposing of GUI Elements

I have several GUI elements added to a JPanel. The JPanel is added to a JScrollPane. The JScrollPane is added to a JFrame (CENTER section of a BorderLayout).
At times I need to remove the JScrollPane and make the space available for other elements. I've provided a method for that. Want to make sure that this method disposes of all resources used by the old JScrollPane and makes them available for Garbage Collection. Please see code below. Is my clearCenter() method sufficient for this task? Is there a better way to do it?
Thanks.
import java.awt.*;
import javax.swing.*;
public class MyGui extends JFrame {
private JScrollPane scroll;
private JPanel panel;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
// Constructor
public MyGui() {
super("Playback");
setSize(250, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
panel = new JPanel();
GridLayout grid = new GridLayout(0, 1, 30, 30);
panel.setLayout(grid);
button1 = new JButton("Button1");
button2 = new JButton("Button2");
button3 = new JButton("Button3");
button4 = new JButton("Button4");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
scroll = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scroll, BorderLayout.CENTER);
add(new JLabel("South", JLabel.CENTER),BorderLayout.SOUTH);
add(new JLabel("North", JLabel.CENTER),BorderLayout.NORTH);
add(new JLabel("East", JLabel.CENTER),BorderLayout.EAST);
add(new JLabel("West", JLabel.CENTER),BorderLayout.WEST);
setVisible(true);
}
public void clearCenter() {
button1 = null;
button2 = null;
button3 = null;
button4 = null;
panel = null;
remove(scroll);
scroll = null;
}
}
At times I need to remove the JScrollPane and make the space available for other elements.
Use a CardLayout as shown in this answer. And I would not worry too much about disposing of a scroll pane, keep it in memory. When it is next needed, update the scroll pane contents then flip back to that card in the layout.
Resetting the content of the scroll-pane can be done like below. Activate the first button to see the button panel replaced by the yellow panel as the view of the scroll-pane. Note that this code is a 'ready to run' MCVE (with a main(String[]) to show it onscreen). Please post MCVE code in future.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyGui extends JFrame {
private JScrollPane scroll;
private JPanel panel;
private JPanel brightPanel;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
// Constructor
public MyGui() {
super("Playback");
setSize(250, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
// create the panel, but don't add it yet.
brightPanel = new JPanel();
brightPanel.setBackground(Color.YELLOW);
panel = new JPanel();
GridLayout grid = new GridLayout(0, 1, 30, 30);
panel.setLayout(grid);
button1 = new JButton("Button1");
button2 = new JButton("Button2");
button3 = new JButton("Button3");
button4 = new JButton("Button4");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
changeViews();
}
};
button1.addActionListener(listener);
scroll = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scroll, BorderLayout.CENTER);
add(new JLabel("South", JLabel.CENTER), BorderLayout.SOUTH);
add(new JLabel("North", JLabel.CENTER), BorderLayout.NORTH);
add(new JLabel("East", JLabel.CENTER), BorderLayout.EAST);
add(new JLabel("West", JLabel.CENTER), BorderLayout.WEST);
setVisible(true);
}
public void changeViews() {
scroll.setViewportView(brightPanel);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new MyGui();
}
};
SwingUtilities.invokeLater(r);
}
}

Put a JTextfield on a JPanel?

Why the textfield is not appearing on my panel which is inside my frame?
I mean is there some additional action necessary to make the components of the panel
visible?
I hope somebody can help me....
public class example1 {
public static void main(String[] args) {
JFrame tt=new TT();
}
}
class TT extends JFrame {
JTextField textField;
JPanel panel;
JButton button1;
JButton button2;
public TT() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Bla Blubb");
setResizable(false);
setLayout(null);
panel=new JPanel();
panel.setBounds(5, 5, 290, 290);
add(panel);
textField=new JTextField();
textField.setBounds(5, 5, 280, 50);
panel.add(textField);
setVisible(true);
}
}
import java.awt.FlowLayout;
import javax.swing.*;
class TT extends JFrame {
JTextField textField;
JPanel panel;
JButton button1;
JButton button2;
public TT() {
//setSize(300, 300); // better to use pack() (after components added)
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // better to use
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//setLocationRelativeTo(null); // better to use..
setLocationByPlatform(true);
setTitle("Bla Blubb");
setResizable(false);
//setLayout(null); // better to use layouts with padding & borders
// set a flow layout with large hgap and vgap.
panel = new JPanel(new FlowLayout(SwingConstants.LEADING, 10, 10));
// panel.setBounds(5, 5, 290, 290); // better to pack()
add(panel);
//textField = new JTextField(); // suggest a size in columns
textField = new JTextField(8);
//textField.setBounds(5, 5, 280, 50); // to get height, set large font
textField.setFont(textField.getFont().deriveFont(50f));
panel.add(textField);
pack(); // make the GUI the minimum size needed to display the content
setVisible(true);
}
public static void main(String[] args) {
// GUIS should be constructed on the EDT.
JFrame tt = new TT();
}
}

Java Open a new window by clicking a button

Been sitting here at my computer for about 13 hours and I think my eyes are bleeding.
I found a little gui editor I love called GuiGenie.
It works perfect for creating the window with the buttons and all that good stuff.
The problem is i want to click a button in my first menu and have it open my other menu i made.
I just starting programming 4 weeks ago so I'm a complete noob.
I have a feeling its messing up because of the main methods but I have no idea and 13 hours of sitting here trying millions of things is making me go crazy : )
here is what i got so far
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
public MyPanel() {
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setBounds (0, 0, 315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel());
frame.pack();
frame.setVisible (true);
}
}
When the button is pressed, I want it to open this new window
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel2());
frame.pack();
frame.setVisible (true);
}
}
If anyone could help I would appreciate it greatly!!
I have a lot of respect for you pros out there because if you are a pro at this, you are probably smarter than 99.9% of the world.
This stuff hurts my brain.
Here is something you can do, for this situation, where you have multiple Forms or Windows what you can do is to use a JPanel which can have this CardLayout set as it's LayoutManager and then you can add the two JPanels to it and access them with the methods provided by the same.
Don't use setBounds() when using Absolute Positioning this is really not the right way of putting components to the parent container. Instead use setLocation(...) and setSize(...) methods. Consider not to use Absolute Positioning as much as possible for you. Certain lines in favour of the before said line taken from Java Docs are as follows :
Although it is possible to do without a layout manager, you should use a
layout manager if at all possible. A layout manager makes it easier to
adjust to look-and-feel-dependent component appearances, to different
font sizes, to a container's changing size, and to different locales.
Layout managers also can be reused easily by other containers, as well as
other programs.
Since the output of your program is really not a soothing experience in any sense. Atleast LayoutManager, can make that work a lot more easier for you, since you need not have to specify position and size for each and every component. Try walking through the Layout Mangers Tutorials, and get accustomed to them as soon as possible. They are the real life savers :-)
Here is a modified code taken from your SOURCE CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
private JPanel contentPane;
public MyPanel(JPanel panel) {
contentPane = panel;
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setLocation(0, 0);
jcomp4.setSize(315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
Here is the code for myPanel class, use this one:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
public MyPanel() {
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
jcomp4.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel2());
frame.pack();
frame.setVisible (true);
}
});
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setBounds (0, 0, 315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel());
frame.pack();
frame.setVisible (true);
}
}

Categories

Resources