First time posting so go easy on me.
I am new to Java and am trying to get 3 JPanels to line up on top of each other. The first image is how I want it to look and it does sometimes when I run the program but as you can see by the other images it doesn't line up every time I run it. Sometimes not even showing some of the images/components.
So how can I get three JPanels to line up one after the other vertically?
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class FrameMain {
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public static void main(String[] args) {
JFrame frame1 = new JFrame("Harvest Frame Test");
frame1.setVisible(true);
frame1.setSize(800,700);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Container Panel
JPanel container = new JPanel();
container.setSize(800,700);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame1.add(container);
//First Panel
JPanel panel1 = new JPanel();
panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel1);
JButton button1 = new JButton("Add Water");
panel1.add(button1);
JButton button2 = new JButton("Add Food");
panel1.add(button2);
JButton button3 = new JButton("Add Medicine");
panel1.add(button3);
ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel = new JLabel(image);
panel1.add(imagelabel);
JProgressBar pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
panel1.add(pbar);
// Second Panel
JPanel panel2 = new JPanel();
panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel2);
JButton button4 = new JButton("Add Water");
panel2.add(button4);
JButton button5 = new JButton("Add Food");
panel2.add(button5);
JButton button6 = new JButton("Add Medicine");
panel2.add(button6);
ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel1 = new JLabel(image1);
panel2.add(imagelabel1);
JProgressBar pbar1 = new JProgressBar();
pbar1.setMinimum(MY_MINIMUM);
pbar1.setMaximum(MY_MAXIMUM);
// add to JPanel
panel2.add(pbar1);
// Third Panel
JPanel panel3 = new JPanel();
panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel3);
JButton button7 = new JButton("Add Water");
panel3.add(button7);
JButton button8 = new JButton("Add Food");
panel3.add(button8);
JButton button9 = new JButton("Add Medicine");
panel3.add(button9);
ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel2 = new JLabel(image2);
panel3.add(imagelabel2);
JProgressBar pbar2 = new JProgressBar();
pbar2.setMinimum(MY_MINIMUM);
pbar2.setMaximum(MY_MAXIMUM);
// add to JPanel
panel3.add(pbar2);
}
//static class Action implements ActionListener {
//public void actionPerformed (ActionEvent e){
//}
//}
}
Move the frame1.setVisible(true); all the way to the bottom. Changing Components on a frame that is already visible can cause issues.
Related
As stated in the title i need to move the label for the text box to be above the box and not to the side. attached i have a picutres of what i mean. what i have vs what i want i have tried searching for it but i cannot seem to find the answer im looking for/not exactly sure what to look up. I have tried using JFrame but it made a separate window unless i need to make the entire GUI a JFrame for me to get the result i want?
Also the actionPerformed method has things but it is irrelevant to the question but displays correctly still.
import java.awt.event.\*;
import javax.swing.\*;
import java.text.DecimalFormat;
public class Project4 extends JFrame implements ActionListener {
private JTextArea taArea = new JTextArea("", 30, 20);
ButtonGroup group = new ButtonGroup();
JTextField name = new JTextField(20);
boolean ch = false;
boolean pep = false;
boolean sup = false;
boolean veg = false;
DecimalFormat df = new DecimalFormat("##.00");
double cost = 0.0;
public Project4() {
initUI();
}
public final void initUI() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
getContentPane().add(panel1, "North");
getContentPane().add(panel2, "West");
getContentPane().add(panel3, "Center");
getContentPane().add(panel4, "East");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
getContentPane().add(panel5, "South");
JButton button = new JButton("Place Order");
button.addActionListener(this);
panel5.add(button);
JButton button2 = new JButton("Clear");
button2.addActionListener(this);
panel5.add(button2);
panel3.add(taArea);
JCheckBox checkBox1 = new JCheckBox("Cheese Pizza") ;
checkBox1.addActionListener(this);
panel4.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Pepperoni Pizza");
checkBox2.addActionListener(this);
panel4.add(checkBox2);
JCheckBox checkBox3 = new JCheckBox("Supreme Pizza");
checkBox3.addActionListener(this);
panel4.add(checkBox3);
JCheckBox checkBox4 = new JCheckBox("Vegetarian Pizza");
checkBox4.addActionListener(this);
panel4.add(checkBox4);
JRadioButton radioButton1 = new JRadioButton("Pick Up");
group.add(radioButton1);
radioButton1.addActionListener(this);
panel1.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Delivery");
group.add(radioButton2);
radioButton2.addActionListener(this);
panel1.add(radioButton2);
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
panel5.add(name_label);
panel5.add(name);
setSize(600, 300);
setTitle("Pizza to Order");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent action) {
}
public static void main(String[] args) {
Project4 ex = new Project4();
ex.setVisible(true);
}
}
You can use a nested JPanel with another layout in order to achieve that. I would go with BorderLayout here. You can also other layouts that allow vertical orientation. Visiting the visual guide to Layout Managers will help you spot them.
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
JPanel verticalNestedPanel = new JPanel(new BorderLayout());
verticalNestedPanel.add(name_label, BorderLayout.PAGE_START);
verticalNestedPanel.add(name, BorderLayout.PAGE_END);
panel5.add(verticalNestedPanel);
I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.
guys I am new to java programming now I have to deal with GUI programming. I have this simple program for admin to create player for now. I have a view class to show the menus, so when user click on create player the interface will be shown.
then within that menu I have a actionListener which goes to another method and show the pop up menu which then show player created successfully. Now the problem is how can I re show the main menu, and not the create player menu.
There will be a few functions for the admin to deal with but now I just have to finish the first function and the rest will be similar. When clicking on the main menu(create player button) it will go to another function(JFrame) to ask for input, again go to another function to getText from the textfield and save it to file
my codes for tpublic void show() {
JFrame frame = new JFrame("Admin");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
panel1 = new JPanel(new GridLayout(6,1));
text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));
button1 = new JButton("Create a player");
button2 = new JButton("Delete a player");
button3 = new JButton("Top up Player's Chips");
button4 = new JButton("Reset Player's password");
button5 = new JButton("Change admin's password");
button6 = new JButton("Logout");
mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
button1.addActionListener(new createPlayerListener());
button1 will got to create player menu
private class createPlayerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
JFrame frame1 = new JFrame("Admin");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JLabel("Create a Player");
text.setFont(new Font("Lucida",Font.PLAIN,24));
mainPanel = new JPanel(new GridLayout(3,1));
mainPanel.setPreferredSize(new Dimension(500,400));
panel1 = new JPanel();
//panel1.setPreferredSize(new Dimension(300,200));
panel1.setLayout(new GridLayout(4,2,10,10));
name = new JLabel("Enter new player name:");
nameTextfield = new JTextField();
pw = new JLabel("Enter new player password:");
pwTextfield = new JTextField();
chip = new JLabel("Enter new player chips:");
chipTextfield = new JTextField();
button1 = new JButton("Create Player");
mainPanel.add(text);
panel1.add(name);
panel1.add(nameTextfield);
panel1.add(pw);
panel1.add(pwTextfield);
panel1.add(chip);
panel1.add(chipTextfield);
mainPanel.add(panel1);
mainPanel.add(button1);
frame1.add(mainPanel);
frame1.pack();
frame1.setVisible(true);
button1.addActionListener(new playerListener());
}
}
private class playerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
String name = nameTextfield.getText();
String pw = pwTextfield.getText();
String chip = chipTextfield.getText();
int chips = Integer.parseInt(chip);
//System.out.println(text);
controller.createPlayer(name, pw, chips);
//pop up window player created
JFrame frame2 = new JFrame();
JOptionPane.showMessageDialog(frame2, "Player Created Successfully!");
}
}
Two issues with your code, creating mainPanel twice, you can do so if you define it locally not globally, otherwise change the names to mainPanel1 and mainPanel2, the second issue is you are calling frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); which closes the application once you close frame1. I tried this code and it works, see the changes I made to it,
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
JPanel panel1 = new JPanel(new GridLayout(6,1));
JLabel text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));
JButton button1 = new JButton("Create a player");
JButton button2 = new JButton("Delete a player");
JButton button3 = new JButton("Top up Player's Chips");
JButton button4 = new JButton("Reset Player's password");
JButton button5 = new JButton("Change admin's password");
JButton button6 = new JButton("Logout");
mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);
add(mainPanel, BorderLayout.CENTER);
button1.addActionListener(new createPlayerListener());
Here is your createPlayerListner modified, the playerListner stays the same,
private class createPlayerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
JFrame frame1 = new JFrame("Admin");
JLabel text = new JLabel("Create a Player");
text.setFont(new Font("Lucida",Font.PLAIN,24));
JPanel mainPanel = new JPanel(new GridLayout(3,1));
mainPanel.setPreferredSize(new Dimension(500,400));
JPanel panel1 = new JPanel();
//panel1.setPreferredSize(new Dimension(300,200));
panel1.setLayout(new GridLayout(4,2,10,10));
JLabel name = new JLabel("Enter new player name:");
nameTextfield = new JTextField();
JLabel pw = new JLabel("Enter new player password:");
pwTextfield = new JTextField();
JLabel chip = new JLabel("Enter new player chips:");
chipTextfield = new JTextField();
JButton button1 = new JButton("Create Player");
mainPanel.add(text);
panel1.add(name);
panel1.add(nameTextfield);
panel1.add(pw);
panel1.add(pwTextfield);
panel1.add(chip);
panel1.add(chipTextfield);
mainPanel.add(panel1);
mainPanel.add(button1);
frame1.add(mainPanel);
frame1.pack();
frame1.setVisible(true);
button1.addActionListener(new playerListener());
}
}
I have created a JPanel that has all the JRadioButtons on it that I need (it is called PortSettings). I also have a button, called port settings, when the user clicks the button, I need the JPanel to come up and display the radio buttons. I have tried to add the JPanel to the actionlistener but it doesn't work. My code is below. I have deleted all other ActionListener's from the other buttons except for the portsettings buttons. If this question is confusing I'm sorry. It's really hard to explain what I need to do. I have uploaded a drawing of what the panel will look like as well as a screenshot of my program.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextArea text = new JTextArea();
JLabel logLabel = new JLabel("Input/Output Log");
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
JRadioButton rs = new JRadioButton("RS232");
JRadioButton usbcdc = new JRadioButton("USB_CDC");
JRadioButton usbhid = new JRadioButton("USB_HID");
JButton next = new JButton("Next");
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(cancel);
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
JButton amount = new JButton("Amount");
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
frame.add(logLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
I have tried to add a JFrame to the ActionListener then add the JPanel to the JFrame but nothing happens when I click the Port Settings button. Also, when I tried to add the JPanel to the JFrame it told me to put final in front of JPanel PortSettings = new JPanel();. Here is the code.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextArea text = new JTextArea();
JLabel logLabel = new JLabel("Input/Output Log");
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
JRadioButton rs = new JRadioButton("RS232");
JRadioButton usbcdc = new JRadioButton("USB_CDC");
JRadioButton usbhid = new JRadioButton("USB_HID");
JButton next = new JButton("Next");
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
final JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(cancel);
JPanel accountButton = new JPanel();
accountButton.add(ok);
accountButton.add(cancel);
JPanel apprvordecl = new JPanel();
apprvordecl.add(apprve);
apprvordecl.add(decline);
JPanel amountButton = new JPanel();
amountButton.add(ok);
amountButton.add(cancel);
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame port = new JFrame("Port Settings");
port.add(PortSettings);
frame.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
JButton amount = new JButton("Amount");
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
frame.add(logLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
You´re on the right track, but you do not want to add your PortSettings panel to a new JFrame but somewhere on your previously built one, assigned to the local variable frame. So your action listener should rather be
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.add(PortSettings, BorderLayout.SOUTH);
frame.pack();
}
});
(This is assuming that you actually want to add it to the frame at that instant and not add it invisibly right from the start and turn it visible, like #Aleksei suggested.)
The error message about final is because you use PortSettings in an (anonymous) inner class - viz., the ActionListener. In my proposed modification the same goes for frame, so you need to adapt its declaration as well:
final JFrame frame = new JFrame();
The reason why is quite technical and beside the point right now: just do it.
If instead you want the panel to appear in a separate window, you need a JDialog for that, not a second JFrame:
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(frame);
dialog.add(PortSettings);
dialog.pack();
dialog.setVisible(true);
}
});
Take a look at the JOptionPane class for a rich choice of ways to get more functionality out of dialogs.
Just add the action listener to all your buttons.
like this:
yourButton.addActionListener(this);
Do that for all the buttons.
Then take your TestPalication class's actionPreformed method and do whatever:
#Override
public void actionPerformed(ActionEvent arg0) {
((JRadioButton) arg0.getSource()).setTitle("Clicked!");
}
Your question was a little bit confusing but I hope this clarifies a little bit.
How do I connect a frame to an existing frame?
The code below is the code for the appletframe. What I want to do is add the other code which is for the frame to be connected to the bottom of the AppletFrame, so that when I drag the Appletframe the frame code with we dragged with it as well. Basically I want the frame code to be attached with the appletFrame so that both the frames are together.
AppletFrame
appletFrame = new JFrame(Settings.serverName);
Loader.webclient = false;
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
appletPanel.setLayout(new BorderLayout());
appletFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/icon.png")));
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, "Center");
appletFrame.pack();
appletFrame.setLocationRelativeTo(null);
appletFrame.setVisible(true);
JMenuBar jmenubar = new JMenuBar();
appletFrame.setJMenuBar(jmenubar);
Layout = new FlowLayout();
ImageIcon keyboard = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/keyboard.png")));
ImageIcon wrench = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/wrench.png")));
Button1 = new JButton("Vote");
Button2 = new JButton("Item List");
Button3 = new JButton("Screenshot");
Button4 = new JButton(wrench);
Button5 = new JButton(keyboard);
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(Layout);
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button1.setText("Vote");
Button2.setText("Item List");
Button3.setText("Screenshot");
Frame which I want it to be attached with the AppletFrame. I want this to be attached to the bottom of the appletFrame, but I don't know how to do it.
JFrame frame = new JFrame();
frame.setSize(775,121);
frame.setResizable(false);
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
As I alluded to in my first comment, this GUI would be better combined into a single top-level container.
Here is an SSCCE1 (mentioned in my 2nd comment) that shows the basic idea, though now I have a better idea of the effect required, the JSplitPane seems less appropriate. Here I just combine the GUI elements into the same layout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGUI extends JPanel {
TestGUI() {
JFrame appletFrame = new JFrame("Settings.serverName");
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
JPanel appletPanel = new JPanel(new BorderLayout());
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, BorderLayout.CENTER);
// Don't use a menu-bar as a tool-bar!
JToolBar jmenubar = new JToolBar();
appletPanel.add(jmenubar, BorderLayout.PAGE_START);
JButton Button1 = new JButton("Vote");
JButton Button2 = new JButton("Item List");
JButton Button3 = new JButton("Screenshot");
JButton Button4 = new JButton("wrench");
JButton Button5 = new JButton("keyboard");
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(new FlowLayout());
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
JTextArea textArea = new JTextArea("TEST", 4, 65 );
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (
textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
appletPanel.add(scroll, BorderLayout.PAGE_END);
appletFrame.pack();
appletFrame.setLocationByPlatform(true);
appletFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new TestGUI();
}
});
}
}
And yes, this would have arrived sooner if I'd had an SSCCE to start with. ;)