The button "closebutton" does not respond. I'm very new to java and I know the error has something to do with actionpreformed in the nested if. I've set up a test to see if words will print the the console when closebutton is pressed. They don't. Any help is appreciated
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myGuiTester implements ActionListener
{
JPanel pane = new JPanel();
JPanel mainPanel = new JPanel();
JFrame MenuFrame = new JFrame("Main Menu");
JFrame Instructions = new JFrame("Instructions");
JFrame game = new JFrame("Game");
JPanel gamepan = new JPanel();
JFrame inst = new JFrame("Instructions");
JPanel instpan = new JPanel(new FlowLayout());
JButton playButton = new JButton("Play");
JButton instButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
JButton closebutton = new JButton("Close");
public static void main(String[] args)
{
new myGuiTester ();
}
public myGuiTester ()
{
MenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inst.setVisible(false);
game.setVisible(false);
JLabel title = new JLabel(" Slot Machine");
JLabel blank = new JLabel("");
JLabel blank1 = new JLabel("");
JLabel blank2 = new JLabel("");
JLabel blank3 = new JLabel("");
playButton.addActionListener(this);
instButton.addActionListener(this);
JLabel game1 = new JLabel("Test");
JLabel inst1 = new JLabel();
exitButton.addActionListener(this);
closebutton.addActionListener(this);
pane.setLayout(new GridLayout(9,1));
pane.add(blank);
pane.add(title);
pane.add(blank1);
pane.add(playButton);
pane.add(blank2);
pane.add(instButton);
pane.add(blank3);
pane.add(exitButton);
mainPanel.add(pane);
MenuFrame.setContentPane(mainPanel);
MenuFrame.setSize(500,400);
MenuFrame.setVisible(true);
//Game Panel
game.setContentPane(gamepan);
game.setSize(500,400);
game1.setText("Game goes here");
gamepan.add(game1);
//Instructions Panel
inst.setContentPane(instpan);
inst.setSize(500,300);
inst1.setText("Instructions go here.");
instpan.add(inst1);
instpan.add(closebutton);
}
//ActionListener
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Play"))
{
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
MenuFrame.setVisible(false);
System.out.print("test play");
}
else if (e.getActionCommand().equals("Instructions"))
{
inst.setVisible(true);
//suspected error
if (e.getActionCommand().equals("Close"))
{
System.out.print("Test inst");
inst.setVisible(false);
}
}
else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
}
You must handle "Close" in the elseif- statement but not inside "Instructions. I should be its own block like:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Play")) {
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
MenuFrame.setVisible(false);
System.out.print("test play");
} else if (e.getActionCommand().equals("Instructions")) {
inst.setVisible(true);
// suspected error
}else if (e.getActionCommand().equals("Close")) {
System.out.print("Test inst");
inst.setVisible(false);
} else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
When you check which button is called you are comparing the ActionCommand of the Event with the label of the button, which are not the same thing :
e.getActionCommand().equals("Close")
What you should do is checking if the button IS the source of the event
e.getSource() == closeButton
Related
I have created a "Main Menu" JFrame with different JButtons for different periods. If I click on one of the buttons a new JFrame opens up but whenever I dispose of it and go back to the main menu and open it back up it it seems as if it more buttons and I don't know why.
Main Menu
public class GUI extends JFrame implements ActionListener {
public static final int WIDTH=1000;
public static final int LENGTH=900;
public static JFrame myFrame = new JFrame("TOK Discussion Participation");
static JPanel myPanel = new JPanel();
public static JComponent buttonPanel;
public static JPanel IPanel = new JPanel();
public static JFrame Period7 = new JFrame("Period 7");
JPanel myPanel1 = new JPanel();
public static Object createPanelPERIOD7;
public JFrame getMyFrame()
{
return myFrame;
}
public void setWindow()
{
//// Creates Icon in frame
JLabel Image = new JLabel();
ImageIcon myIcon = new ImageIcon(new ImageIcon("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg").getImage().getScaledInstance(200, 200, 1));
Image.setIcon(myIcon);
//set window
Color backgroundColor = new Color(5,149,251);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setBounds(550,200,1000,600);
myFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
myFrame.setLayout(new BorderLayout());
myFrame.add(myPanel1,BorderLayout.NORTH);
myFrame.add(new JLabel(new ImageIcon("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg")), BorderLayout.CENTER);
//myFrame.setIconImage("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg");
myFrame.getContentPane().setBackground(backgroundColor);
myFrame.setVisible(true);
//set label
JLabel myLabel = new JLabel("TOK Discussion Participation");
JLabel myLabel1= new JLabel("");
JLabel myLabel2 = new JLabel("");
//set button (PERIOD 7)
JButton Period_7_BUTTON = new JButton("Period 7");
//set Listener (PERIOD 7)
Period_7_BUTTON.addActionListener(new GUI());
//2nd button (PERIOD 5)
JButton Period_5_BUTTON = new JButton("Period 5");
//set Listener PERIOD 5)
Period_5_BUTTON.addActionListener(new GUI());
//2nd button (PERIOD 2)
JButton Period_2_BUTTON = new JButton("Period 2");
//set Listener PERIOD 2)
Period_2_BUTTON.addActionListener(new GUI());
//2nd button (PERIOD 2)
JButton Period_4_BUTTON = new JButton("Period 4");
//set Listener PERIOD 4)
Period_4_BUTTON.addActionListener(new GUI());
//Setting Button FONT/SIZE
Period_2_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
Period_4_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
Period_5_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
Period_7_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
//Bundle button to panel
myPanel1.setLayout(new BoxLayout(myPanel1, BoxLayout.X_AXIS));
myPanel1.add(Period_7_BUTTON);
myPanel1.add(Period_5_BUTTON);
myPanel1.add(Period_2_BUTTON);
myPanel1.add(Period_4_BUTTON);
//add content to window and make it visible
myFrame.add(myPanel1,BorderLayout.NORTH);
myFrame.setVisible(true);
}
public void dispose(){
myFrame.dispose();
}
Other JFrame that duplicates buttons once reopened
//Method to create Period 7 Frame
public static void createFramePERIOD7() {
//Creates Panel + Buttons
JPanel P7_Panel = new JPanel();
JButton Column = new JButton("Add Columns");
JButton Insert = new JButton("Insert Student 7");
JButton Back = new JButton("Home");
//Adding Buttons and Layout to Period 7 Frame
Period7.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Period7.setBounds(550,200,1000,600);
Period7.setLayout(new FlowLayout());
Period7.add(Back);
Period7.add(Column);
Period7.add(Insert);
//Add ActionListener to Buttons
Back.addActionListener(new GUI());
Insert.addActionListener(new GUI());
Column.addActionListener(new GUI());
//Adding Panel to Frame
//Period7.add(P7_Panel);
Period7.setVisible(true);
}
And finally my ending listener code
public void actionPerformed(ActionEvent e)
{
String buttonCommand = e.getActionCommand();
//Main menu Period 7 button
if(buttonCommand.equals("Period 7"))
{
myFrame.dispose();
createFramePERIOD7();
}
if(buttonCommand.equals("Home"))
{
setWindow();
Period7.dispose();
}
Your method adds 3 buttons everytime... You can fix it by changing your
createFramePERIOD7() method
By inserting the line indicated below.
public static void createFramePERIOD7() {
//Creates Panel + Buttons
JPanel P7_Panel = new JPanel();
JButton Column = new JButton("Add Columns");
JButton Insert = new JButton("Insert Student 7");
JButton Back = new JButton("Home");
Period7 = new JFrame(); //<-------------------------- Insert this line
//Adding Buttons and Layout to Period 7 Frame
Period7.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Period7.setBounds(550,200,1000,600);
Period7.setLayout(new FlowLayout());
Period7.add(Back);
Period7.add(Column);
Period7.add(Insert);
//Add ActionListener to Buttons
Back.addActionListener(new GUI());
Insert.addActionListener(new GUI());
Column.addActionListener(new GUI());
//Adding Panel to Frame
//Period7.add(P7_Panel);
Period7.setVisible(true);
}
I'm trying to make a sort of simple soccer simulator. This is the code I created after watching tutorials and i know its pretty bad. All i want to do is add a value to the team, like 1 for the best team and 10 for the worst, and when i click simulate a pop up would show up telling me which team would win given the teams value. But i cant figure out how to do it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class sim extends JPanel {
public sim() {
// JFrame constructor
super(true);
JRadioButton chelsea, arsenal, chelsea2, arsenal2;
this.setLayout(new GridLayout(3,0));
ButtonGroup group = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
// takes image and saves it the the variable
Icon a = new ImageIcon(getClass().getResource("a.PNG"));
Icon c = new ImageIcon(getClass().getResource("c.JPG"));
chelsea = new JRadioButton("Chelsea",c);
chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal = new JRadioButton("Arsenal",a);
arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);
group.add(chelsea);
group.add(arsenal);
JLabel label = new JLabel("");
TitledBorder titled = new TitledBorder("Team 1");
label.setBorder(titled);
chelsea.setBorder(titled);
arsenal.setBorder(titled);
JButton button = new JButton("Simulate");
button.setHorizontalAlignment(JButton.CENTER);
add(button, BorderLayout.CENTER);
chelsea2 = new JRadioButton("Chelsea",c);
chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal2 = new JRadioButton("Arsenal",a);
arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);
group2.add(chelsea2);
group2.add(arsenal2);
JLabel label2 = new JLabel("");
TitledBorder titled2 = new TitledBorder("Team 2");
label2.setBorder(titled2);
chelsea2.setBorder(titled);
arsenal2.setBorder(titled);
add(label);
add(chelsea);
add(arsenal);
add(button);
add(chelsea2);
add(arsenal2);
HandlerClass handler = new HandlerClass();
chelsea.addActionListener(handler);
}
private class HandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, String.format("%s", e.getActionCommand()));
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setContentPane(new sim());
frame.setVisible(true);
}
}
Firstly capitalize the name of your class always in Java, then check this code :
public class Sim extends JPanel {
public Sim() {
// JFrame constructor
super(true);
JRadioButton chelsea, arsenal, chelsea2, arsenal2;
this.setLayout(new GridLayout(3,0));
ButtonGroup group = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
// takes image and saves it the the variable
Icon a = new ImageIcon("/home/mehdi/Pictures/ICONS/Test/1.png");
Icon c = new ImageIcon("/home/mehdi/Pictures/ICONS/Test/2.png");
chelsea = new JRadioButton("Chelsea",c);
chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal = new JRadioButton("Arsenal",a);
arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);
group.add(chelsea);
group.add(arsenal);
final JLabel label = new JLabel("");
TitledBorder titled = new TitledBorder("Team 1");
label.setBorder(titled);
chelsea.setBorder(titled);
arsenal.setBorder(titled);
JButton button = new JButton("Simulate");
button.setHorizontalAlignment(JButton.CENTER);
add(button, BorderLayout.CENTER);
chelsea2 = new JRadioButton("Chelsea",c);
chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal2 = new JRadioButton("Arsenal",a);
arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);
group2.add(chelsea2);
group2.add(arsenal2);
JLabel label2 = new JLabel("");
TitledBorder titled2 = new TitledBorder("Team 2");
label2.setBorder(titled2);
chelsea2.setBorder(titled);
arsenal2.setBorder(titled);
add(label);
add(chelsea);
add(arsenal);
add(button);
add(chelsea2);
add(arsenal2);
button.addActionListener(new HandlerClass(label));
}
private class HandlerClass implements ActionListener{
final JLabel label;
public HandlerClass(JLabel label){
this.label = label;
}
public void actionPerformed(ActionEvent e)
{
label.setText("SSSSSSSSSSSSSsssss");
JOptionPane.showMessageDialog(null, "Something");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setContentPane(new Sim());
frame.setVisible(true);
}
}
public class StatsGUI extends JFrame implements ActionListener {
JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;
public StatsGUI()
{
JPanel panel = new JPanel();
label = new JLabel("Enter number");
panel.add(label);
input = new JTextField(10);
input.addActionListener(this);
panel.add(input);
label2 = new JLabel("Statistics");
output = new JTextField(10);
output.setEditable(false);
panel.add(output);
getButton = new JButton("Go");
getButton.addActionListener(this);
panel.add(getButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
panel.add(exitButton);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exitButton)
{
System.exit(0);
}
else
{
String text = input.getText();
output.setText(text + "COUNTER");
}
}
public static void main(String[] args)
{
}
This is my simple GUI program. I have placed all buttons and other gadgets within the constructor. However, I am not sure what I should be putting inside my main in order to get my GUI to actually show up. I am sure I am missing something incredibly simple here however I am not sure what. Help would be much appreciated.
You are not that far from getting things to work. Just a few things to know:
Your UI should be started on the Event dispatching thread (EDT)
You actually need to add your panels/components to your frame
You need to pack() your Window/Frame
You need to make it visible
(Design stuff, optional but when you are at it, why not just fix that as well), no need to extend JFrame, so let's just drop that.
So eventually, taking these advices into consideration leads you to something like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class StatsGUI implements ActionListener {
JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
System.exit(0);
} else {
String text = input.getText();
output.setText(text + "COUNTER");
}
}
public void initUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
label = new JLabel("Enter number");
panel.add(label);
input = new JTextField(10);
input.addActionListener(this);
panel.add(input);
label2 = new JLabel("Statistics");
output = new JTextField(10);
output.setEditable(false);
panel.add(output);
getButton = new JButton("Go");
getButton.addActionListener(this);
panel.add(getButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
panel.add(exitButton);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new StatsGUI().initUI();
}
});
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class StatsGUI extends JFrame implements ActionListener {
JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;
public StatsGUI()
{
JPanel panel = new JPanel();
label = new JLabel("Enter number");
panel.add(label);
input = new JTextField(10);
input.addActionListener(this);
panel.add(input);
label2 = new JLabel("Statistics");
output = new JTextField(10);
output.setEditable(false);
panel.add(output);
getButton = new JButton("Go");
getButton.addActionListener(this);
panel.add(getButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
panel.add(exitButton);
add(panel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exitButton)
{
System.exit(0);
}
else
{
String text = input.getText();
output.setText(text + "COUNTER");
}
}
public static void main(String[] args)
{
StatsGUI s= new StatsGUI();
s.setVisible(true);
s.setSize(1000,1000);
}
}
You are not calling a contructor .call costructor like 'StatsGUI s= new StatsGUI();' or 'new StatsGUI();' .
I am trying to display the drawing that I have posted. When my code runs and the user clicks Account, the panel only displays the buttons "OK" and "Cancel" (see screenshot). I have added three JTextAreas with a JLabel for each to the panel accountPanel but they don't display. My code is below.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
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) {
JLabel input = new JLabel();
final JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.add(input);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
ethernet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog esettings = new JDialog(frame);
esettings.setTitle("Ethernet Settings");
esettings.setSize(400, 400);
esettings.pack();
esettings.setVisible(true);
}
});
JRadioButton rs = new JRadioButton("RS232");
rs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog rsettings = new JDialog(frame);
rsettings.setTitle("RS232 Settings");
rsettings.setSize(400, 400);
rsettings.pack();
rsettings.setVisible(true);
}
});
JRadioButton usbcdc = new JRadioButton("USB_CDC");
usbcdc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbc = new JDialog(frame);
usbc.setTitle("USB_CDC Settings");
usbc.setSize(400, 400);
usbc.pack();
usbc.setVisible(true);
}
});
JRadioButton usbhid = new JRadioButton("USB_HID");
usbhid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbh = new JDialog(frame);
usbh.setTitle("USB_HID Settings");
usbh.setSize(400, 400);
usbh.pack();
usbh.setVisible(true);
}
});
JButton next = new JButton("Next");
JButton aok = new JButton("OK");
JButton bok = new JButton("OK");
JButton cok = new JButton("OK");
JButton acancel = new JButton("Cancel");
JButton bcancel = new JButton("Cancel");
JButton ccancel = new JButton("Cancel");
JButton dcancel = new JButton("Cancel");
JLabel cardLabel = new JLabel("Card Number: ");
JLabel expLabel = new JLabel("Exp. Date: ");
JLabel cvvLabel = new JLabel("CVV: ");
JTextArea card = new JTextArea();
card.add(cardLabel);
JTextArea expDate = new JTextArea();
expDate.add(expLabel);
JTextArea cvv = new JTextArea();
cvv.add(cvvLabel);
final JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(bcancel);
final JPanel accountPanel = new JPanel();
accountPanel.add(bok);
accountPanel.add(ccancel);
accountPanel.add(card);
accountPanel.add(expDate);
accountPanel.add(cvv);
final JPanel apprvordecl = new JPanel();
apprvordecl.add(apprve);
apprvordecl.add(decline);
apprvordecl.add(aok);
apprvordecl.add(acancel);
final JPanel amountPanel = new JPanel();
amountPanel.add(cok);
amountPanel.add(dcancel);
input.setFont(new java.awt.Font("Tahoma", 3, 18));
input.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
input.setText("Input / Output Log");
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) {
JDialog port = new JDialog(frame);
port.setTitle("Port Settings");
port.setSize(400, 400);
port.add(PortSettings);
port.pack();
port.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");
account.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog accountDialog = new JDialog(frame);
accountDialog.setTitle("Account");
accountDialog.setSize(400, 400);
accountDialog.add(accountPanel);
accountDialog.pack();
accountDialog.setVisible(true);
}
});
JButton amount = new JButton("Amount");
amount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog amount2 = new JDialog(frame);
amount2.setTitle("Amount");
amount2.setSize(400, 400);
amount2.add(amountPanel);
amount2.pack();
amount2.setVisible(true);
}
});
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
approvordecl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog apprv = new JDialog(frame);
apprv.setTitle("Approve / Decline");
apprv.setSize(400, 400);
apprv.add(apprvordecl);
apprv.pack();
apprv.setVisible(true);
}
});
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");
ButtonGroup group = new ButtonGroup();
group.add(usbhid);
group.add(usbcdc);
group.add(ethernet);
group.add(rs);
ButtonGroup approvegroup = new ButtonGroup();
approvegroup.add(apprve);
approvegroup.add(decline);
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 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
}
}
JPanel uses FlowLayout by default which respects preferred sizes. The default width of the preferred size for the JTextArea's is 0 x 0. You need to give the JTextComponent's a preferred size for them to appear. Use the constructor that specifys rows & columns:
JTextArea card = new JTextArea(5, 10);
Don't forget to make the JTextArea scrollable by enclosing it in a JScrollPane:
accountPanel.add(new JScrollPane(card));
I tried using:
frame1.getContentPane().setBackground(Color.yellow);
But it is not working. Can anyone help me?
import java.awt.*;
import java.awt.Color;
public class PlayGame {
public static void main(String[] args) {
GameFrame frame1 = new GameFrame();
frame1.getContentPane().setBackground(Color.yellow);
// Set Icon
Image icon = Toolkit.getDefaultToolkit().getImage("image/poker_icon.gif");
frame1.setIconImage(icon);
frame1.setVisible(true);
frame1.setSize(600, 700);
frame1.setTitle("Card Game");
// Set to exit on close
frame1.setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);
}
}
GameFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameFrame extends JFrame implements ActionListener {
private JPanel topPnl, btmPnl, pcPnl, mainPnl;
private JPanel titlePnl, playerPnl, computerPnl;
private JLabel titleLbl, playerLbl, computerLbl;
private JLabel testTextBox1, testTextBox2;
private ImageIcon playerIcon, computerIcon;
//
private JPanel pickCardPnl, pickCardTitlePnl, cardPnl, resultPnl, optionPnl;
private JLabel pickCardTitleLbl;
private JLabel card1Lbl, card2Lbl, card3Lbl, card4Lbl, card5Lbl;
private JLabel resultLbl;
private JButton restartBtn, showCardBtn, exitBtn;
private JButton card1Btn, card2Btn, card3Btn, card4Btn, card5Btn;
private ImageIcon card1Pic, card2Pic, card3Pic, card4Pic, card5Pic;
private JButton playerBtn, computerBtn;
private ImageIcon playerPic;
private String[] card = new String[53];
private String name;
// ArrayInt al;
public GameFrame() {
// a1 = new Array();
//a1.generateRandom();
setCard();
setName();
// Top Panel /////////////////////////////////////
mainPnl = new JPanel(new BorderLayout());
topPnl = new JPanel(new BorderLayout(50, 0));
titlePnl = new JPanel();
pcPnl = new JPanel(new GridLayout(1, 2, 10, 10));
playerPnl = new JPanel(new BorderLayout(10, 10));
computerPnl = new JPanel(new BorderLayout(10, 10));
// Title Panel
titleLbl = new JLabel("Card Game");
titlePnl.add(titleLbl);
// Player Panel
playerIcon = new ImageIcon("image/player.png");
playerLbl = new JLabel(name, playerIcon, JLabel.CENTER);
playerPnl.add(playerLbl, BorderLayout.NORTH);
playerPic = new ImageIcon("image/unknwon.png");
playerBtn = new JButton(playerPic);
playerPnl.add(playerBtn, BorderLayout.CENTER);
playerBtn.setContentAreaFilled(false);
playerBtn.setBorder(BorderFactory.createEmptyBorder());
// Computer Panel
computerIcon = new ImageIcon("image/computer.png");
computerLbl = new JLabel("Computer:", computerIcon, JLabel.CENTER);
computerPnl.add(computerLbl, BorderLayout.NORTH);
playerPic = new ImageIcon("image/back.png");
computerBtn = new JButton(playerPic);
computerPnl.add(computerBtn, BorderLayout.CENTER);
computerBtn.setContentAreaFilled(false);
computerBtn.setBorder(BorderFactory.createEmptyBorder());
pcPnl.add(playerPnl);
pcPnl.add(computerPnl);
// Add panel into Top Panel
topPnl.add(titlePnl, BorderLayout.NORTH);
topPnl.add(pcPnl, BorderLayout.CENTER);
// Bottom Panel /////////////////////////////////////
btmPnl = new JPanel(new BorderLayout());
pickCardPnl = new JPanel(new BorderLayout());
pickCardTitlePnl = new JPanel();
cardPnl = new JPanel(new GridLayout(1, 5, 5, 5));
resultPnl = new JPanel();
optionPnl = new JPanel(new GridLayout(1, 3, 5, 5));
// Pick Card Panel
pickCardTitleLbl = new JLabel("Pick Your Card:");
pickCardPnl.add(pickCardTitleLbl, BorderLayout.NORTH);
card1Pic = new ImageIcon(card[1]);
card1Btn = new JButton(card1Pic);
cardPnl.add(card1Btn);
card1Btn.addActionListener(this);
card2Pic = new ImageIcon(card[2]);
card2Btn = new JButton(card2Pic);
cardPnl.add(card2Btn);
card2Btn.addActionListener(this);
card3Pic = new ImageIcon(card[3]);
card3Btn = new JButton(card3Pic);
cardPnl.add(card3Btn);
card3Btn.addActionListener(this);
card4Pic = new ImageIcon(card[4]);
card4Btn = new JButton(card4Pic);
cardPnl.add(card4Btn);
card4Btn.addActionListener(this);
card5Pic = new ImageIcon(card[5]);
card5Btn = new JButton(card5Pic);
cardPnl.add(card5Btn);
card5Btn.addActionListener(this);
// new ImageIcon(a1.getRandomNumber);
pickCardPnl.add(cardPnl, BorderLayout.CENTER);
card1Btn.setContentAreaFilled(false);
card1Btn.setBorder(BorderFactory.createEmptyBorder());
card2Btn.setContentAreaFilled(false);
card2Btn.setBorder(BorderFactory.createEmptyBorder());
card3Btn.setContentAreaFilled(false);
card3Btn.setBorder(BorderFactory.createEmptyBorder());
card4Btn.setContentAreaFilled(false);
card4Btn.setBorder(BorderFactory.createEmptyBorder());
card5Btn.setContentAreaFilled(false);
card5Btn.setBorder(BorderFactory.createEmptyBorder());
// Result Panel
setCard();
resultLbl = new JLabel("adasdadadasdasdasdasd");
resultPnl.add(resultLbl);
// Option Panel
restartBtn = new JButton("Restart");
optionPnl.add(restartBtn);
restartBtn.addActionListener(this);
showCardBtn = new JButton("Show Cards");
optionPnl.add(showCardBtn);
showCardBtn.addActionListener(this);
exitBtn = new JButton("Exit");
optionPnl.add(exitBtn);
exitBtn.addActionListener(this);
// Add panel into Bottom Panel
btmPnl.add(pickCardPnl, BorderLayout.NORTH);
btmPnl.add(resultPnl, BorderLayout.CENTER);
btmPnl.add(optionPnl, BorderLayout.SOUTH);
//
mainPnl.add(topPnl, BorderLayout.NORTH);
// add(midPNL, BorderLayout.CENTER);
mainPnl.add(btmPnl, BorderLayout.CENTER);
add(mainPnl);
// Menu bar
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Game");
menuBar.add(menu);
JMenuItem item3 = new JMenuItem("Change Name");
item3.addActionListener(this);
menu.add(item3);
JMenuItem item = new JMenuItem("Change Card Deck");
item.addActionListener(this);
menu.add(item);
JMenu subMenu = new JMenu("Change BackGround");
subMenu.addActionListener(this);
menu.add(subMenu);
JMenuItem subItem = new JMenuItem("Blue");
subItem.addActionListener(this);
subMenu.add(subItem);
JMenuItem subItem2 = new JMenuItem("Green");
subItem2.addActionListener(this);
subMenu.add(subItem2);
//
menu.addSeparator();
//
JMenuItem item4 = new JMenuItem("Quit");
item4.addActionListener(this);
menu.add(item4);
setJMenuBar(menuBar);
} //End of GameFrame
public void setCard() {
GenRandom g1 = new GenRandom();
g1.GenRandomCard();
int[] allCard = new int[11];
allCard = g1.getAllCard();
for (int i = 1; i <= 10; i++) {
card[i] = "image/card/" + allCard[i] + ".png";
}
}
public void setName() {
// name = JOptionPane.showInputDialog(null, "Please Enter Your Name", "Welcome", JOptionPane.QUESTION_MESSAGE) + ":";
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == card1Btn) {
playerBtn.setIcon(card1Pic);
card1Btn.setEnabled(false);
}
if (e.getSource() == card2Btn) {
playerBtn.setIcon(card2Pic);
card2Btn.setEnabled(false);
}
if (e.getSource() == card3Btn) {
playerBtn.setIcon(card3Pic);
card3Btn.setEnabled(false);
}
if (e.getSource() == card4Btn) {
playerBtn.setIcon(card4Pic);
card4Btn.setEnabled(false);
}
if (e.getSource() == card5Btn) {
playerBtn.setIcon(card5Pic);
card5Btn.setEnabled(false);
}
if (e.getSource() == restartBtn) {
new AePlayWave("sound/jet.wav").start();
JOptionPane.showMessageDialog(null, "Restart Button ");
}
if (e.getSource() == exitBtn) {
/* long start = System.currentTimeMillis();
long end = start + 4 * 1000; // 60 seconds * 1000 ms/sec
while (System.currentTimeMillis() < end) {
// run
new AePlayWave("sound/jet.wav").start();
}*/
System.exit(0);
}
}
}
Since you did not post an SSCCE, I will do it for you. This shows how to change the background color of a JFrame. Starting from this, you can start adding components to the JFrame and see where you go wrong, instead of letting us look at a few hundred lines of code.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.EventQueue;
public class ColoredFrame {
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame( "TestFrame" );
frame.getContentPane().setBackground( Color.PINK );
//frame contains nothing, so set size
frame.setSize( 200, 200 );
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} );
}
}
Just put your setVisible(true); at the end of your constructor.
Moreover you had added mainPnl on your JFrame, so changing colour of the JFrame will be useless,
so instead of writing
add(mainPnl);
in your GameFrame class, you better be using
setContentPane(mainPnl);
for frame1.getContentPane().setBackground(Color.YELLOW); to work.
Hope this might help
Regards
You should give background color to JPanel and then use this JPanel in your JFrame rather than giving direct background to your JFrame.
I know this is a very old question, but for others that are looking for the right answer this could also be written as following:
frame1.getContentPane().setBackground(new Color (255,255,102)); //or whatever color you want in the RGB range