Could you please help me use the ActionListener correclty in my code? The code compiles and the GUI is displayed correctly, but no button works!! If you want to test the code, note that you need to put the image in the same folder as the project file created and change the line "ImageIcon myImageIcon = new ImageIcon("rodeo.jpg");" according to the name of your photo.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageApplication extends JFrame implements ActionListener
{
public Image myImage;
public JLabel myImageLabel;
public ImageIcon myImageIcon;
public JFrame frame;
public JTextField txtWidth, txtHeight;
public int origWidth, origHeight;
public static void main(String[] args)
{
int origWidth, origHeight;
ImageApplication ia = new ImageApplication();
ia.setVisible(true);
JFrame frame = new JFrame();
ImageIcon myImageIcon = new ImageIcon("rodeo.jpg");
JLabel myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);
Image myImage = myImageIcon.getImage();
origWidth = myImageIcon.getIconWidth();
origHeight = myImageIcon.getIconHeight();
JMenuBar myMenuBar = new JMenuBar();
JMenu myMenu = new JMenu("Options");
JMenuItem myMenuItem1 = new JMenuItem("Double");
JMenuItem myMenuItem2 = new JMenuItem("Reset");
myMenu.add(myMenuItem1);
myMenu.add(myMenuItem2);
myMenuBar.add(myMenu);
ia.setJMenuBar(myMenuBar);
JButton bAL = new JButton("Align Left");
JButton bAC = new JButton("Align Center");
JButton bAR = new JButton("Align Right");
JButton bResize = new JButton ("Resize");
bAL.setFocusPainted(false);
bAC.setFocusPainted(false);
bAR.setFocusPainted(false);
bResize.setFocusPainted(false);
JLabel lWidth = new JLabel("Width:");
JLabel lHeight = new JLabel("Height:");
JTextField txtWidth = new JTextField(Integer.toString(origWidth));
JTextField txtHeight = new JTextField(Integer.toString(origHeight));
JPanel GRID = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1f;
c.weighty = 0f;
c.gridx = 0;
c.gridy = 0;
GRID.add(bAL, c);
c.gridx++;
GRID.add(bAC, c);
c.gridx++;
GRID.add(bAR, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
GRID.add(myImageLabel, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
GRID.add(lWidth, c);
c.gridx++;
c.gridwidth = 2;
GRID.add(txtWidth, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 3;
GRID.add(lHeight, c);
c.gridx++;
c.gridwidth = 2;
GRID.add(txtHeight, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 4;
GRID.add(bResize, c);
ia.add(GRID, BorderLayout.CENTER);
ia.setSize(origWidth + 150, origHeight + 150);
myMenuItem1.addActionListener(ia);
myMenuItem1.setActionCommand("double");
myMenuItem2.addActionListener(ia);
myMenuItem2.setActionCommand("reset");
bAL.addActionListener(ia);
bAL.setActionCommand("left");
bAC.addActionListener(ia);
bAC.setActionCommand("center");
bAR.addActionListener(ia);
bAR.setActionCommand("right");
bResize.addActionListener(ia);
bResize.setActionCommand("resize");
}
private void ResizeImage(int Width, int Height)
{
myImage = myImage.getScaledInstance(Width, Height, Image.SCALE_SMOOTH);
myImageIcon.setImage(myImage);
myImageLabel.setIcon(myImageIcon);
txtWidth.setText(Integer.toString(Width));
txtHeight.setText(Integer.toString(Height));
setSize(Width + 150, Height + 150);
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command == "left") myImageLabel.setHorizontalAlignment(JLabel.LEFT);
else if(command == "center") myImageLabel.setHorizontalAlignment(JLabel.CENTER);
else if(command == "right") myImageLabel.setHorizontalAlignment(JLabel.RIGHT);
else if(command == "resize") ResizeImage(Integer.parseInt(txtWidth.getText()),
Integer.parseInt(txtHeight.getText()));
else if(command == "double") ResizeImage(myImageIcon.getIconWidth() * 2,
myImageIcon.getIconHeight() * 2);
else if(command == "reset") ResizeImage(origWidth, origHeight);
}
}
Use String#equals to compare String content. You are using the == operator which compares Object references.
However, as the buttons have differing functionality, better for each to have an individual ActionListener. This can be done using an anonymous ActionListener instance.
Side issue: The class member variable myImageLabel is not being assigned. Rather another variable
with the same name is initialized in the static main method. You need to move all the components instantiated in the main method into an instance method and also remove the JLabel local class declaration.
After moving code:
JLabel myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);
should be
myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);
Try this method:
What it does: it adds to the button itself, when clicked an method to be performed:
buttonName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//do what ever
}
});
//set bunds for the button itself, if not done otherwise, but for your layout.
These instance variables:
public JTextField txtWidth, txtHeight;
are never initialized but are referred to in your listener code. You have local variables of the same name that you're instantiating. Change this:
JTextField txtWidth = new JTextField(Integer.toString(origWidth));
JTextField txtHeight = new JTextField(Integer.toString(origHeight));
to this:
txtWidth = new JTextField(Integer.toString(origWidth));
txtHeight = new JTextField(Integer.toString(origHeight));
and similarly for your other instance variables.
Use the following code to get Image
ImageIcon myImageIcon = new ImageIcon("rodeo.jpg").getImage();
Related
package committeeGUI;
import static committeeGUI.CommitteeGUI.comList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StudentMemberFrame extends JFrame {
public StudentMemberFrame() {
super("Add Student");
setSize(450, 500);
setLocation(561, 150);
super.setResizable(false);
addStudentMember();
}
public void addStudentMember() {
CommitteeGUI.frame.setEnabled(false);
final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
showConsoleArea.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//creating border and size of the border
showConsoleArea.setBorder(BorderFactory.createLineBorder(Color.black, 3));
add(showConsoleArea); //, BorderLayout.CENTER);
//setting a size to showConsoleArea.
showConsoleArea.setSize(500, 500);
final JLabel lblheading = new JLabel("STUDENT");
// showConsoleArea.add(lblheading,BorderLayout.CENTER);
/*
* creating components of company form
*/
final JLabel lblCommitteeName = new JLabel("Committee name");
final JTextField txtName = new JTextField(15);
final JLabel lblMemberName = new JLabel("Student name");
final JTextField txtMemberName = new JTextField(15);
final JLabel lblMemberNumber = new JLabel("Student number");
final JTextField txtMemberNumber = new JTextField(15);
final JLabel lblMemberCourse = new JLabel("Student course");
final JTextField txtMemberCourse = new JTextField(15);
final JButton buttAdd = new JButton("SAVE");
final JButton buttCancel = new JButton("CANCEL");
// adding components to the display area
c.gridx = 1;
c.gridy = 0;
showConsoleArea.add(lblheading, c);
c.gridx = 0;
c.gridy = 1;
showConsoleArea.add(lblCommitteeName, c);
c.gridx = 1;
c.gridy = 1;
showConsoleArea.add(txtName, c);
c.gridx = 0;
c.gridy = 2;
showConsoleArea.add(lblMemberName, c);
c.gridx = 1;
c.gridy = 2;
showConsoleArea.add(txtMemberName, c);
c.gridx = 0;
c.gridy = 3;
showConsoleArea.add(lblMemberNumber, c);
c.gridx = 1;
c.gridy = 3;
showConsoleArea.add(txtMemberNumber, c);
c.gridx = 0;
c.gridy = 4;
showConsoleArea.add(lblMemberCourse, c);
c.gridx = 1;
c.gridy = 4;
showConsoleArea.add(txtMemberCourse, c);
c.gridx = 0;
c.gridy = 5;
showConsoleArea.add(buttAdd, c);
c.gridx = 1;
c.gridy = 5;
showConsoleArea.add(buttCancel, c);
/*
* able to displaying the company frame
*/
this.show();
buttAdd.addActionListener((ActionEvent e) -> {
if (txtName.getText().equals("")
|| txtMemberName.getText().equals("")
|| txtMemberNumber.getText().equals("")
|| txtMemberCourse.getText().equals("")) //validating the data
{
CommitteeGUI.frame.setEnabled(false);
setEnabled(false);
messagebox("Enter a valid data", 0);
return;
}
if (!txtMemberNumber.getText().matches("\\d+")) {
CommitteeGUI.frame.setEnabled(false);
setEnabled(false);
messagebox("Member number must be a integer", 0);
return;
}
for (Committee com : comList) {
if (com.getName().equals(txtName.getText())) {
Student st = new Student();
st.setName(txtMemberName.getText());
st.setAcademicNo(Integer.parseInt(txtMemberNumber.getText()));
st.setCourse(txtMemberCourse.getText());
com.memberList.add(st);
messagebox("Member added successfully", 1);
setEnabled(false);
return;
}
}
messagebox("No Committee found with given name", 1);
});
//creating ActionListner to Cancel button
buttCancel.addActionListener((ActionEvent e) -> {
//frame is enabled for user.
CommitteeGUI.frame.setEnabled(true);
dispose(); //disposing the frame
} //pass the action to actionPerformed method and perform it.
);
}
#SuppressWarnings("deprecation")
public void messagebox(String label, final int conform) {
final JDialog infoBox = new JDialog();//message box
infoBox.setSize(400, 90);
infoBox.setAlwaysOnTop(true);
infoBox.setResizable(false);
infoBox.setLocation(675, 258);
JLabel space = new JLabel(" ");
JLabel label1 = new JLabel(label);
JButton buttOk = new JButton("Ok");
buttOk.addActionListener((ActionEvent e) -> {
if (conform == 1) {
// making frame operation enable.
CommitteeGUI.frame.setEnabled(true);
dispose();
}
setEnabled(true);
infoBox.hide();
});
JPanel holder = new JPanel(new FlowLayout());
holder.add(label1);
holder.add(buttOk);
infoBox.add(holder);
infoBox.show();
}
}
Above is my code. I want to put space between the heading (STUDENT) and the fields.
Attached is the snapshot of the frame:
I am not familiar with this layout. Help is much appreciated.
public void addStudentMember() {
final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
showConsoleArea.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Add below line of code change appropriate spacing
c.insets = new Insets(10, 10, 10, 10);
I need help in Java, I can't insert 2 JPanel in one frame with different and preferred size, this is the code:
public class canvas extends JFrame {
public final String TITLE = "test"; /* Game's title */
public static int wWIDTH = 800; /* WIDTH of the window's games */
public static int wHEIGHT = 600; /* HEIGHT of the window's game */
/* => GAME ENGINEERING ATTRIBUTES <= */
private Dimension WINDOW_SIZE;
private Container c;
private rightpane rightpane;
private leftpane leftpane;
public canvas() {
Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
WINDOW_SIZE = new Dimension(wWIDTH, wHEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle(TITLE);
this.setUndecorated(false);
this.setSize(WINDOW_SIZE);
this.setResizable(false);
this.setLocation(((SCREEN_SIZE.width / 2) - (wWIDTH / 2)),
((SCREEN_SIZE.height / 2) - (wHEIGHT / 2)));
leftpane = new leftpane();
rightpane = new rightpane();
//leftpane.setPreferredSize(leftpane_dim);
this.add(rightpane,BorderLayout.EAST);
this.add(leftpane,BorderLayout.WEST);
this.setVisible(true);
}
and rightpane and leftpane class:
public class rightpane extends JPanel {
public JLabel lb;
public rightpane() {
this.setBackground(Color.YELLOW);
lb = new JLabel();
lb.setText("right paneeeeeeeee");
this.add(lb);
}
public void paintComponents(Graphics g) {
super.paintComponents(g);
}
}
public class leftpane extends JPanel {
public JLabel lb;
public leftpane() {
this.setBackground(Color.BLACK);
lb = new JLabel();
lb.setText("left pane");
this.add(lb);
}
public void paintComponents(Graphics g) {
super.paintComponents(g);
}
}
This is the result:
I tried with GridBagLayout:
GridBagLayout gl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
this.setLayout(gl);
c.ipadx = 200;
c.gridwidth = 2;
c.gridx = c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
c.gridheight = 3;
gl.setConstraints(leftpane, c);
this.add(new leftpane());
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 3;
c.anchor = GridBagConstraints.EAST;
gl.setConstraints(rightpane, c);
this.add(new rightpane());
And other solutions found in the network but still not working. Can anyone help me?
There were a few problems in your GridBagLayout. Here's the fixed version
GridBagLayout gl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
this.setLayout(gl);
c.fill = GridBagConstraints.BOTH; // <== need to set fill
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
c.weightx = 1; // <== need to set weight
c.weighty = 1; // <== need to set weight
// gl.setConstraints(leftpane, c); // <== constraints get overridden by your add() call...
this.add(new leftpane(), c); // pass constraints in here
c.gridx = 1;
c.weightx = 3; <== need to set weight, note different value, space shared in ratio 1:3
c.anchor = GridBagConstraints.EAST; // pass constraints in here
Picture
I'm running into a problem, I believe with the actionlistener. The weird thing is that it works on some computers and not on others. All computers are running JE7.1. So, on some systems, it works flawlessly, the user enters text, presses enter and the text is appended to the JTextPane and updates the static userInput variable. On other computers though, text is appended, but the variable never seems to update.
For example:
user types in "Hello"
"Hello is appened to the JtextPane and the static variable userInput = "Hello"
Am I missing something very silly here? Thank you in advance.
package com.core;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.text.*;
public class GUI {
public static JFrame f;
private static JPanel topPanel = new JPanel();
private static JTextPane textArea = new JTextPane();
public static JTextField inputText = new JTextField();
private static String userInput = "";
private static Player player;
public static JPanel sidePanel = new JPanel();
public JFrame buildFrame(){
f = new JFrame("AMSA World");
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
textArea.setFocusable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String text = inputText.getText();
if(!text.equals("")){
appendToPane(text + "\n\n", Color.blue);
inputText.setText("");
if(text.startsWith("use ")){
ArrayList<Item> inventory = player.getInventory();
String key = text.substring(4);
boolean found = false;
for(Item i : inventory){
if(i.getName().equalsIgnoreCase(key)){
found = true;
i.use();
break;
}
}
if(!found){
appendToPane("Item does not exist in your iventory.\n\n", Color.black);
}
}
if(text.startsWith("inventory")){
ArrayList<Item> inventory = player.getInventory();
if(inventory.size() > 0){
for(Item i : inventory){
appendToPane(i.getName() +"\n", Color.darkGray);
}
}
else
appendToPane("Your iventory is empty.\n\n", Color.black);
appendToPane("#", Color.blue);
}
else{
userInput = text;
}
}
}
};
inputText.addActionListener(listener);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5,5,5,5);
c.weightx = 1.0;
c.gridwidth = 3;
c.weighty = 0.025;
c.gridx = 0;
c.gridy = 0;
f.add(topPanel, c);
c.fill = GridBagConstraints.BOTH;
c.weighty = 1.0;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 1;
f.add(scrollPane, c);
c.fill = GridBagConstraints.BOTH;
c.weighty = 1.0;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 1;
f.add(sidePanel, c);
sidePanel.setVisible(false);
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.025;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 2;
f.add(inputText, c);
f.setVisible(true);
return f;
}
public static JTextField getTextField(){
return inputText;
}
public static void toggleTextField(boolean value){
inputText.setEnabled(value);
}
public static String getUserInput(){
return userInput;
}
public static void setUserInput(String s){
userInput = s;
}
public static JPanel getTopPanel(){
return topPanel;
}
public static JPanel getSidePanel(){
return sidePanel;
}
public static void setPlayer(Player p){
player = p;
}
public static void appendToPane(String msg, Color c){
if(inputText.isEnabled()){
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
textArea.setCharacterAttributes(aset, false);
textArea.replaceSelection(msg);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
}
To (try) to answer your question, I'm guessing you are running into multi-thread issues. Swing events run in their own thread thread, the Event Dispatch Thread. If you are accessing the variable from another thread, you will run into occasional situations where the variables are not synchronized between threads.
In the bigger picture, taking shortcuts in writing Java code will generally end up costing you a lot of time and effort. Placing your entire UI in static variables with static methods is not a good idea. Take the time to instantiate an instance. Beyond that, I'm guessing you didn't start the UI using SwingUtilities.invokeLater(). Read the Swing Tutorial, which I linked above. Swing works well, but it is not simple.
As of right now I have all my labels and input fields stacked up the center of the JFrame.
When I run the program the program, the label Business Name appears top center and nothing else appears till I resize the window...
Can anyone tell me why it does this and how to fix it?
Also I would like to add a area to the left side of the JFrame that will display the contact when you click save, what type of component would I use to do this?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import javax.swing.*;
public class Driver extends JFrame {
private JFrame f;
private JPanel p;
private JTextField fieldBN;
private JTextField fieldFN;
private JTextField fieldLN;
private JTextField fieldP;
private JTextField fieldE;
private JTextField fieldA;
private JTextField aLine2;
private JTextField fieldW;
private JLabel labelBN;
private JLabel labelFN;
private JLabel labelLN;
private JLabel labelP;
private JLabel labelE;
private JLabel labelA;
private JLabel labelW;
private JButton buttonS;
// Constructor:
public Driver() {
gui();
}
public void gui() {
f = new JFrame("Contact Book");
f.setVisible(true);
f.setSize(900,800); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10, 10, 10, 10);
p = new JPanel(new GridBagLayout());
f.getContentPane().add(p, BorderLayout.NORTH);
c.gridx = 100;
c.gridy = 0;
labelBN = new JLabel ("Business Name");
p.add(labelBN, c);
c.gridx = 110;
c.gridy = 0;
fieldBN = new JTextField(10);
p.add(fieldBN, c);
c.gridx = 100;
c.gridy = 10;
labelFN= new JLabel ("First Name");
p.add(labelFN, c);
c.gridx = 110;
c.gridy = 10;
fieldFN = new JTextField (10);
p.add(fieldFN, c);
c.gridx = 100;
c.gridy = 20;
labelLN= new JLabel ("Last Name");
p.add(labelLN, c);
c.gridx = 110;
c.gridy = 20;
fieldLN = new JTextField (10);
p.add(fieldLN, c);
c.gridx = 100;
c.gridy = 30;
labelP = new JLabel ("Phone Number");
p.add(labelP, c);
c.gridx = 110;
c.gridy = 30;
fieldP = new JTextField (10);
p.add(fieldP, c);
c.gridx = 100;
c.gridy = 40;
labelE = new JLabel ("Email");
p.add(labelE, c);
c.gridx = 110;
c.gridy = 40;
fieldE = new JTextField (10);
p.add(fieldE, c);
c.gridx = 100;
c.gridy = 50;
labelA = new JLabel ("Address");
p.add(labelA, c);
c.gridx = 110;
c.gridy = 50;
fieldA = new JTextField (10);
p.add(fieldA, c);
c.gridx = 110;
c.gridy = 60;
aLine2 = new JTextField (10);
p.add(aLine2, c);
c.gridx = 100;
c.gridy = 70;
labelW = new JLabel ("Website");
p.add(labelW, c);
c.gridx = 110;
c.gridy = 70;
fieldW = new JTextField (10);
p.add(fieldW, c);
c.gridx = 110;
c.gridy = 80;
buttonS = new JButton("Save");
p.add(buttonS, c);
// Window Listeners
addWindowListener(new WindowAdapter() {
}
);
} // End Gui
public static void main(String[] args) {
new Driver();
} // End main Method
} // End class Driver
The probable cause is right here...
public void gui() {
f = new JFrame("Contact Book");
f.setVisible(true);
f.setSize(900,800); // default size is 0,0
You're calling setVisible to early.
The simple solution is to call setVisible once you've set the frame up the way you want...
public void gui() {
f = new JFrame("Contact Book");
// Add everything to the frame...
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
}
Also, extending from JFrame and then creating an instance of JFrame is very confusing. Generally, you shouldn't need to extend from JFrame, just keep creating an instance as you need.
You should also take a look at Initial Threads
For example...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
I have the following simple code and I don't know how to modify it so as to have 3 separate panels to switch to, one for each button:
package TouristLocations;
import javax.swing.*;
import java.awt.*;
public class buildApp extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setSize(400,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel title = new JLabel("Locations");
title.setFont(new Font("Serif", Font.BOLD, 40));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
frame.add(title, c);
JButton b1 = new JButton("View Locations");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
frame.add(b1, c);
JButton b2 = new JButton("Insert Locations");
c.gridx = 1;
c.gridy = 1;
frame.add(b2, c);
JButton b3 = new JButton("Help");
c.gridx = 2;
c.gridy = 1;
frame.add(b3, c);
TextArea text1 = new TextArea(15,40);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
frame.add(text1, c);
frame.pack();
}
}
thank you
Sounds like you should consider using JTabbedPane.
In addition to How to Use Tabbed Panes, you may want to look at CardLayout, mentioned here and here.
You should create a main container:
JPanel mainContainer = new JPanel();
//creation of each child
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
... so each button must add those panels into the main container and resize new panel size, something like this:
//for button1:
mainContainer.add(panel1);
panel1.setSize(mainContainer.getSize());
... for button2 action, you must follow the same way above.