Working on adding a GUI to my simple craps simulation program.
Made a new JPanel and added a few JTextFields to it with a default text value. I get no error, and the code runs, but all I get is a blank window with nothing in it.
Here is the code:
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;
public class CrapsGUI extends JFrame
{
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField die1 = new JTextField("Die 1",30);
JTextField die2 = new JTextField("Die 2",30);
JTextField sum = new JTextField("Sum",30);
JTextField point = new JTextField("Point",30);
JTextField status = new JTextField("Status",30);
public CrapsGUI()
{
setTitle("Craps Simulator 2013");
setVisible(true);
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp.add(die1);
jp.add(die2);
jp.add(sum);
jp.add(point);
jp.add(status);
}
public static void main(String[] args)
{
Craps craps = new Craps();
CrapsGUI crapsGUI = new CrapsGUI();
}
}
Thanks in advance!
You haven't added the JPanel that contains the visible components.
add(jp);
Three things come to mind
Make sure you are starting your UI's from within the context of the Event Dispatching Thread. See Initial Threads for more details
Call setVisible only after you have finished creating your UI
It would also be helpful if you added something to you frame. Try using add(jp)
try this one:
public static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Sample Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// first text box
JPanel textbox1Panel = new JPanel();
textbox1Panel.setLayout(new BoxLayout(textbox1Panel, 0));
textbox1Panel.setOpaque(true);
textbox1Panel.setBackground(new Color(100, 0, 131));
textbox1Panel.setPreferredSize(new Dimension(300, 300));
textbox1Panel.add(die1);
textbox1Panel.add(die2);
textbox1Panel.add(sum);
textbox1Panel.add(point);
// Set the menu bar and add the label to the content pane.
frame.getContentPane().add(textbox1Panel, BorderLayout.SOUTH);
// Display the window.
frame.pack();
frame.setVisible(true);
}
Related
There's a simple application that uses JScrollPane for 2 lists and have 1 button to switch them. I want to add many more Swing elements, but I cannot move them with object.setBounds. Whatever I will write in this method element doesn't change its place and size.
package paka;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;;
public class Question extends JFrame {
private JList leftlist,rightlist;
private JButton movebutton;
private JLabel pointlessLabel;
private static String[] foods={"bacon","wings","ham","beef","more bacon"};
public Question(){
super("title");
setLayout(new FlowLayout());
leftlist=new JList(foods);
leftlist.setVisibleRowCount(3);
leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
movebutton = new JButton("Move");
movebutton.addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
rightlist.setListData(leftlist.getSelectedValues());
}
}
);
add(movebutton);
rightlist = new JList();
rightlist.setVisibleRowCount(3);
rightlist.setFixedCellWidth(100);rightlist.setFixedCellHeight(15);
rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(rightlist));
//Okay, deleting everything below we have only two list with button that moves elements from 1st list to 2nd
movebutton = new JButton("Click me!");
movebutton.setBounds(700, 100, 80, 20);
add(movebutton);
pointlessLabel = new JLabel("I'm unbreakable");
pointlessLabel.setBounds(500,200,100,50);
add(pointlessLabel);
}
public static void main(String args[]){
Question go = new Question();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
You need to use combinations of Layout Managers (as stated by #AndrewThompson on his comment above) to achieve the same output that you have with a null layout.
Avoid the use of null layout, see: Null layout is evil and Why is it frowned upon to use a null layout in Swing?
With the code below you can have the same output you had with setBounds() method. I didn't added an ActionListener to this code, since I'm just demonstrating how to stop using null layout and have the same output. Yes! The second one seems shorter, that's because of pack() but you can still setSize() if you want / need an specific window size.
To add more elements below just add more JPanels and add them to pane, I hope this helps to solve your issue, and if not, please post a Minimal Complete and Verifiable Example that we can copy-paste, make it short, but still shows your issue and follows above recommendations
Important Note:
I'll bring a quote from This answer's comment of #MadProgrammer, because I used prototypeCellValue to make the rightList's width shorter:
I'd also be careful with prototypeCellValue, unless the value matches the expected length of your data, it could truncate your data when it's displayed, just need to be careful
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class QuestionTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
String[] foods = { "bacon", "wings", "ham", "beef", "more bacon" };
JFrame frame;
JPanel topPane, bottomPane, pane;
JList leftList, rightList;
JButton moveButton, clicMeButton;
JScrollPane scroll;
JLabel label;
frame = new JFrame("title");
topPane = new JPanel();
bottomPane = new JPanel();
pane = new JPanel();
leftList = new JList(foods);
rightList = new JList();
moveButton = new JButton("Move");
clicMeButton = new JButton("Click me!");
label = new JLabel("I'm unbreakable");
leftList.setVisibleRowCount(3);
rightList.setVisibleRowCount(3);
leftList.setPrototypeCellValue(String.format("%30s", ""));
rightList.setPrototypeCellValue(String.format("%30s", ""));
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
topPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
scroll = new JScrollPane(leftList);
topPane.add(scroll);
topPane.add(moveButton);
scroll = new JScrollPane(rightList);
topPane.add(scroll);
bottomPane.setLayout(new FlowLayout());
bottomPane.add(clicMeButton);
bottomPane.add(label);
pane.add(topPane);
pane.add(bottomPane);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I have a JFrame with a JButton , this button open a new JFrame where there should be a text box ( JTextField ) that I will use for a search , the problem is that I don't know how to insert it . I came up with this :
N.B I'm a beginner, sorry in advance for the easy question :)
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;
public class MainWindow {
// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {
panel.setLayout(null);
JButton Ricerca = new JButton("Ricerca");
Ricerca.setBounds(100, 100, 200, 50);
panel.add(Ricerca);
Ricerca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
FinestradiRicerca.setBounds(300, 300, 500, 500);
FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel riquadroRicerca = new JPanel();
FinestradiRicerca.add(riquadroRicerca);
FinestradiRicerca.setVisible(true);
JTextField ciao;
ciao = new JTextField ();
}
});
}
//Main
public static void main(String[] args) {
//Finestra Principale
JFrame finestra = new JFrame("Finestra principale");
finestra.setSize(500, 500);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel della finestra principale
JPanel riquadro = new JPanel();
finestra.add(riquadro);
finestra.setVisible(true);
NuovaFinestra(riquadro);
}
}
You needed to add your new elements to riquadroRicerca BEFORE adding the Panel to FinestradiRicerca, I recommend you NOT to use null layout but a Layout Manager or combinations of them. If you insist on keeping null layout then see below example. But for this kind of app I'd suggest a CardLayout.
I also suggest not using multiple JFrames since they will open multiple windows on task bar which is user unfriendly. See: Use of multiple JFrames, Good / Bad Practice
As an aside note, follow Java naming conventions. For example you called a JFrame as FinestradiRicerca instead rename it to: finestradiRicerca (1st letter of a variable in lowercase).
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;
public class MainWindow {
// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {
panel.setLayout(null);
JButton Ricerca = new JButton("Ricerca");
Ricerca.setBounds(100, 100, 200, 50);
panel.add(Ricerca);
Ricerca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
FinestradiRicerca.setBounds(300, 300, 500, 500);
//If you don't want to close whole app when closing this windo change it to: JFrame.DISPOSE_ON_CLOSE
FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel riquadroRicerca = new JPanel();
JTextField ciao;
JLabel myLabel = new JLabel("Here goes your label text");
ciao = new JTextField ();
ciao.setColumns(20);
riquadroRicerca.add(myLabel);
riquadroRicerca.add(ciao);
FinestradiRicerca.add(riquadroRicerca);
FinestradiRicerca.setVisible(true);
}
});
}
//Main
public static void main(String[] args) {
//Finestra Principale
JFrame finestra = new JFrame("Finestra principale");
finestra.setSize(500, 500);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel della finestra principale
JPanel riquadro = new JPanel();
finestra.add(riquadro);
finestra.setVisible(true);
NuovaFinestra(riquadro);
}
}
So, your code after a few modifications, to make JLabel and JTextField visible gives the following output:
However, please follow my above recomendations.
Add the JTextField to your new JFrame like this. You also have to initialize your textfield. It is basically the same as you have done with the initial JFrame.
JTextField ciao = new JTextField();
FinestradiRicerca.add(ciao);
I was playing around with JFrame today and had trouble getting a JButton component to display text properly. The text displayed in JButton is cut off at the end. I tried resizing the JButton component in order to make sure that the text could fit, but the same problem occurred. The problem looks like this:
Here is the code:
import javax.swing.JFrame;
public class Launcher {
public static void main(String[] args) {
JFrame jFrame = new JFrame("Frame");
jFrame.setSize(400, 400);
jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
jFrame.setLocation(400, 400);
jFrame.add(new Drawable(jFrame));
jFrame.setVisible(true);
}
}
Here is the other class in another .java file.
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawable extends JPanel {
private JButton button;
private JFrame jFrame;
public Drawable(JFrame jFrame) {
this.jFrame = jFrame;
button = new JButton("This text does not show properly");
button.setPreferredSize(new Dimension(200, 25));
button.setLocation(jFrame.getWidth() / 2 - 50, jFrame.getHeight() / 2 - 12);
this.add(button);
}
}
I understand that this might be a problem with my project's setup, so if anyone needs me to post it I can do so.
Remove the statement
button.setPreferredSize(new Dimension(200, 25));
which is being used by the panel's layout manager to constrain the button width
I'm a complete noobie with swing. I'm trying to set a few JPanels and TextAreas to show up but after spending 2 days reading the APIs and trying to add panels to frames and textareas to panels and nothing is showing up.. I'm utterly confused. If anyone could explain how is the best way to do this I would be very grateful
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout()); // J FRAME
JPanel panel = new JPanel(); // first panel on the left
panel.setLayout(new BoxLayout(panel, 1));
// frame.getContentPane().setBackground(Color.red);
frame.add(panel);
JLabel surname = new JLabel();
JLabel initial = new JLabel();
JLabel ext = new JLabel();
surname.setOpaque(true);
initial.setOpaque(true);
ext.setOpaque(true);
frame.add(surname);
panel.add(initial);
panel.add(ext);
JTextArea table = new JTextArea();
table.setEditable(false);
panel.add(table);
table.setVisible(true);
You're adding stuff to the JFrame after it's already visible. If you do that, you need to revalidate your JFrame so it knows to redo its layout.
You could also just wait to show your JFrame until after you've added everything.
Edit: Here is an example program that shows what I'm talking about. Try running this, then take out the call to revalidate() to see the difference.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton show = new JButton("Show");
show.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent showE) {
frame.add(new JLabel("Test"), BorderLayout.SOUTH);
frame.revalidate(); //tell the JFrame to redo its layout!
}
});
frame.add(show);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
You are adding an empty elements like:
JLabel surname = new JLabel();
Your elements is already added but have nothing to be display.
Try :
JLabel surname = new JLabel("UserName");
JLabel initial = new JLabel("Iinitial");
JLabel ext = new JLabel("Ext");
JTextArea table = new JTextArea(10, 5);
How to create a game menu with three interfaces,1st interface has two option exit and continue,choose team and continue or back,3rd playing interface
package SimpleSoccer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
/** * * #author Andyblem */ public class TopLevelDemo {
static JButton startButton = new JButton("START");
static JButton exitButton = new JButton("EXIT");
static JButton backButton = new JButton("MENU");
static JPanel panel = new JPanel(new FlowLayout());
static JFrame frame;
private static void createAndShowGUI(){
frame = new JFrame("Top Level Demo");
frame.setSize(400,400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar cyanMenuBar = new JMenuBar();
cyanMenuBar.setOpaque(true);
cyanMenuBar.setBackground(Color.cyan);
cyanMenuBar.setPreferredSize(new Dimension(200,180));
JLabel yellowLabel = new JLabel();
yellowLabel.setOpaque(true);
yellowLabel.setBackground(Color.yellow);
yellowLabel.setPreferredSize(new Dimension(200,20));
frame.setJMenuBar(cyanMenuBar);
frame.getContentPane().add(panel);
panel.add(startButton);
panel.add(exitButton);
// frame.getContentPane().add(exitButton);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]){
createAndShowGUI();
}
}
Your "question" (if you could even call it that) is kind of unclear. Seems like you just don't know where to begin. So I'll give you a tip.
What I would do is:
Use a CardLayout. What the layout does is "layer" panels, making them navigable with methods like show(pickAPanelToShow), next(nextPanel), and previous(previousPanel).
What you can do is on first page have a the two buttons, if continue is pressed, then the next() method can take you to the chooseTeamPanel. And from that panel you can navigate to the gamePanel after the team is chosen.
You can see more at How to use CardLayout and you can see an example here