I am trying to create a UI for an imaginary vehicle that has both Automatic and Manual modes. When the user sets the vehicle into one of the modes, it should only display the controls relevant to that mode, and I've accomplished this using a CardLayout.
However, I'd also like to be able to specify the location of the various elements of the layout for each card manually - for a static layout I'd do something along the lines of mainPanel.setLayout(null), but this simply gives a blank window when used on a CardLayout (hence the two commented-out lines in the code below).
How would I achieve both of these things? My current code is below:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UI extends JFrame implements ActionListener{
public UI() {
initUI();
}
private JPanel cardPanel;
private CardLayout cardLayout = new CardLayout();
public final void initUI() {
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
JPanel manualPanel = new JPanel();
getContentPane().add(manualPanel);
//manualPanel.setLayout(null);
cardPanel.add(manualPanel, "manual");
JPanel autoPanel = new JPanel();
//autoPanel.setLayout(null);
cardPanel.add(autoPanel, "auto");
JButton startButton = new JButton("START/STOP");
startButton.setBounds(50, 150, 200, 50);
startButton.addActionListener(new startListener());
manualPanel.add(startButton);
autoPanel.add(startButton);
JButton autoButton = new JButton("SWITCH TO AUTO");
autoButton.setBounds(50, 250, 200, 50);
autoButton.addActionListener(new autoListener());
manualPanel.add(autoButton);
JButton upButton = new JButton("^");
upButton.setBounds(125, 320, 50, 50);
upButton.addActionListener(new returnListener());
manualPanel.add(upButton);
JButton downButton = new JButton("\\/");
downButton.setBounds(125, 380, 50, 50);
downButton.addActionListener(new returnListener());
manualPanel.add(downButton);
JButton ccwButton = new JButton("<-");
ccwButton.setBounds(55, 350, 50, 50);
ccwButton.addActionListener(new returnListener());
manualPanel.add(ccwButton);
JButton cwButton = new JButton("->");
cwButton.setBounds(195, 350, 50, 50);
cwButton.addActionListener(new returnListener());
manualPanel.add(cwButton);
JButton ngzButton = new JButton("SOMETHING ELSE");
ngzButton.setBounds(50, 450, 200, 50);
ngzButton.addActionListener(new returnListener());
manualPanel.add(ngzButton);
JButton manualButton = new JButton("SWITCH TO MANUAL");
manualButton.setBounds(50, 250, 200, 50);
manualButton.addActionListener(new manualListener());
autoPanel.add(manualButton);
JButton returnButton = new JButton("SOMETHING ELSE");
returnButton.setBounds(50, 350, 200, 50);
returnButton.addActionListener(new returnListener());
autoPanel.add(returnButton);
setTitle("UI");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(cardPanel, BorderLayout.NORTH);
}
public static void main(String[] args) {
UI ui = new UI();
ui.setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
private class returnListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class autoListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "auto");
}
}
private class startListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class manualListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "manual");
}
}
}
In your example, you create a startButton, but you then attempt to add the same instance to two different panels. Because a component can only occupy one container, you'll need to create two buttons, one for each panel.
As an aside, instead of using a null layout, give each panel BorderLayout, add the buttons to a JPanel having the default FlowLayout, and add the button panel to the SOUTH. You can then nest your illustrations in the CENTER using whatever layout is appropriate.
Addendum: As #Frakcool comments, using a layout will improve the cross-platform appearance of your buttons. Invoke pack() on the enclosing window, and override getPreferredSize() on the nested illustration panel to give it the needed size. In this related example, the CENTER panel is used for drawing only; having no components, its layout then becomes irrelevant.
Related
I making a game but I have a main meny and too outer menys and I want to open one of the menys by clicking on the respective button in the JFrame.
code of main.java
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import game.client.mainClient;
import game.server.mainServer;
public class main extends JPanel {
/**
*
*/
private static final long serialVersionUID = 6590770928148744094L;
private JLabel jcomp1;
private JButton jcomp5;
private JButton jcomp6;
private JLabel jcomp8;
private JLabel jcomp9;
private JLabel jcomp10;
public main() {
//construct components
jcomp1 = new JLabel ("test game");
jcomp5 = new JButton ("singel player");
jcomp6 = new JButton ("multiplayer");
jcomp8 = new JLabel ("this game is made by kebe_");
jcomp9 = new JLabel ("gui is made in guigenie");
jcomp10 = new JLabel ("game verision dev 1.0");
//adjust size and set layout
setPreferredSize (new Dimension (681, 466));
setLayout (null);
//add components
add (jcomp1);
add (jcomp5);
add (jcomp6);
add (jcomp8);
add (jcomp9);
add (jcomp10);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (295, 5, 70, 25);
jcomp5.setBounds (265, 30, 115, 30);
jcomp6.setBounds (265, 65, 115, 30);
jcomp8.setBounds (0, 430, 180, 25);
jcomp9.setBounds (0, 415, 155, 20);
jcomp10.setBounds (0, 445, 140, 25);
// close and open
// singleplayer
jcomp5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
mainClient mc = new mainClient();
mc.setVisible(true);
}
});
// multiplayer
jcomp6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
mainServer ms = new mainServer();
ms.setVisible(true);
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Test game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new main());
frame.pack();
frame.setVisible (true);
}
}
code of mainClient.java
package game.client;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class mainClient extends JPanel
{
/**
*
*/
private static final long serialVersionUID = -1271816540338950462L;
private JLabel jcomp1;
private JComboBox jcomp2;
private JButton jcomp3;
public mainClient() {
//construct preComponents
String[] jcomp2Items = {"save 1", "save 2", "save 3", "save 4", "save 5"};
//construct components
jcomp1 = new JLabel ("singel player");
jcomp2 = new JComboBox (jcomp2Items);
jcomp3 = new JButton ("play selected save");
//adjust size and set layout
setPreferredSize (new Dimension (681, 466));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (290, 5, 85, 25);
jcomp2.setBounds (280, 30, 100, 25);
jcomp3.setBounds (255, 70, 150, 25);
jcomp3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Singel player");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new mainClient());
frame.pack();
frame.setVisible (true);
}
}
code of mainServer
package game.server;
import java.awt.*;
import javax.swing.*;
public class mainServer extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 2726545572728204122L;
private JLabel jcomp1;
private JButton jcomp2;
private JButton jcomp3;
public mainServer() {
//construct components
jcomp1 = new JLabel ("multiplayer");
jcomp2 = new JButton ("host game");
jcomp3 = new JButton ("join game");
//adjust size and set layout
setPreferredSize (new Dimension (681, 466));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (300, 0, 75, 25);
jcomp2.setBounds (285, 25, 100, 25);
jcomp3.setBounds (285, 50, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Multiplayer");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new mainServer());
frame.pack();
frame.setVisible (true);
}
}
how can I open the JFrame in mainClient.java when I click on the singleplayer button, and the JFrame in mainServer.java when I click on the multiplayer button?
Create ActionListeners for your buttons. Define your frames, and you can access them with your class name. For example;
class mainClient extends JPanel {
JFrame mainClientFrame = new JFrame();
}
and then;
mainClient mc = new mainClient();
mc.mainClientFrame.setVisible(true);
if you want to use this frames in your main void, define them static.
class mainClient extends JPanel {
static JFrame mainClientFrame = new JFrame();
}
So i have 3 classes.The first one is for creating a frame :
public class DrawingFrame extends JFrame{
public DrawingFrame(){
JFrame abc = new JFrame("TEST");
abc.setSize(600,500);
abc.setLayout(null);
abc.setVisible(true);
abc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolbar top = new Toolbar();
abc.add(top);
}
}
The second one is for JPanel :
public class Toolbar extends JPanel{
public Toolbar(){
JPanel top = new JPanel();
top.setLayout(null);
top.setVisible(true);
top.setPreferredSize(new Dimension(150,150));
top.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
top.add(buton);
}
}
And this is the main class:
public class Main {
public static void main(String[] args) {
DrawingFrame a = new DrawingFrame();
}
}
My code prints out the frame but not with the panel.How i can fix this ?
First, you don't need to create an instance JPanel in a subclass of JPanel, same for JFrame. The instance is already one.
Use this to access the instance itself :
public Toolbar(){
this.setLayout(null);
this.setVisible(true);
this.setPreferredSize(new Dimension(150,150));
this.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
this.add(buton);
}
Second, if you use a null layout, you need to set the bounds of each componennt, as mention in Doing Without a Layout Manager (Absolute Positioning)
Creating a container without a layout manager involves the following steps.
Set the container's layout manager to null by calling setLayout(null).
Call the Component class's setbounds method for each of the container's children.
Call the Component class's repaint method.
So adding the bounds to the JPanel will be enough with : top.setBounds(0,0,150,150); for example
class DrawingFrame extends JFrame{
public DrawingFrame(){
super("TEST");
this.setSize(600,500);
this.setLayout(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolbar top = new Toolbar();
top.setBounds(0, 0, 150, 150);
this.add(top);
}
}
class Toolbar extends JPanel{
public Toolbar(){
this.setLayout(null);
this.setVisible(true);
this.setPreferredSize(new Dimension(150,150));
this.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
this.add(buton);
}
}
And this will look like what you asked (in term of dimension and absolute position)
Move abc.setVisible(true); to the end
Also, use this instead of create new panel in toolbar. Same is in the case of frame as well
So, the below code will work:
public static void main(String...s) {
DrawingFrame a = new DrawingFrame();
}
class DrawingFrame extends JFrame{
public DrawingFrame(){
super("TEST");
this.setLayout(new FlowLayout());
Toolbar top = new Toolbar();
this.add(top);
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Toolbar extends JPanel{
public Toolbar(){
this.setLayout(new FlowLayout());
this.setPreferredSize(new Dimension(150,150));
this.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
this.add(buton);
this.setVisible(true);
}
}
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());
}
I want the KeyEvent to change the background color of my JPanels. Nothing happens when I press anything on the keyboard. One of my applications specifications is that I need a 'Customized component extended from JPanel.' which is why I have another class for my graphics panel.
My problem is when G is pressed nothing happens but my center panel should turn green...
Here is code for part of my application.
public class Maths extends JFrame implements KeyListener
{
private JPanel pNorth = new JPanel();
private JPanel pSouth = new JPanel();
private JPanel pCenter = new JPanel();
private JPanel pEast = new JPanel();
private JPanel pWest = new JPanel();
private File file;
private JPanel pDraw = new GraphicsPanel();
public static void main(String args[])
{
new Maths();
}
public Maths()
{
mainFrame = new JFrame();
mainFrame.setTitle("Maths Test Game");
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize(1200, 800);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(pNorth, BorderLayout.NORTH);
mainFrame.add(pSouth, BorderLayout.SOUTH);
mainFrame.add(pCenter, BorderLayout.CENTER);
mainFrame.add(pEast, BorderLayout.EAST);
mainFrame.add(pWest, BorderLayout.WEST);
pNorth.setLayout(new FlowLayout());
pSouth.setLayout(new FlowLayout());
pCenter.setLayout(new FlowLayout());
pEast.setLayout(new FlowLayout());
pWest.setLayout(new FlowLayout());
addKeyListener(this);
setFocusable(true);
mainFrame.setVisible(true);
}
class GraphicsPanel extends JPanel
{
GraphicsPanel()
{
// set a preferred size for the custom panel.
setPreferredSize(new Dimension(250, 300));
}
#Override
public void paint(Graphics g)
{
super.paint(g);
// set blue color for drawing
g.setColor(Color.blue);
// face
g.drawOval(90, 70, 80, 80);
// eyes
g.drawOval(110, 95, 5, 5);
g.drawOval(145, 95, 5, 5);
// nose
g.drawLine(130, 95, 130, 115);
// mouth
g.drawArc(113, 115, 35, 20, 0, -180);
}
}
#Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_G)
{
pCenter.setBackground(Color.green);
}
repaint();
}
#Override
public void keyReleased(KeyEvent e)
{
}
#Override
public void keyTyped(KeyEvent e)
{
}
}
There are a multitude of reasons this is probably not working, generally, you don't want to attach KeyListeners to top level containers like JFrame, as they is simply to many things that can get in the way and prevent the frame from raising key events.
Instead, use the key bindings API. See How to Use Key Bindings for more details
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);
}
}