JFrame does not show anything - swing

I am trying to make a RPG game in a GUI and it's not going so well.
Before I added the JButton everything worked and displayed in the window as it should. I'm not sure what happened after I added the JButton.
There is supposed to be the title displayed in the grey area and the button in the blue area. Ive tried running normally and running with debugger, none of the text or button shows up.
I am following the tutorial here step-by-step and I dont see anything out of place. (I know that i've changed the variable names).
What have I done wrong here? Do I need to add anything extra?
import javax.swing.*;
import java.awt.*;
public class Game {
JFrame window;
Container c;
JPanel titlePanel;
JPanel startButtonPanel;
JLabel titleLabel;
JButton startButton;
Font titleFont = new Font("Cooper Black", Font.PLAIN, 90);
Font buttonFont = new Font("Cooper Black", Font.PLAIN, 32);
public static void main(String[] args) {
new Game();
}
public Game() {
//Main Window
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.BLACK);
window.setLayout(null);
window.setVisible(true);
c = window.getContentPane();
//Title Panel
titlePanel = new JPanel();
titlePanel.setBounds(100, 100, 600, 150);
titlePanel.setBackground(Color.GRAY);
titleLabel = new JLabel("TEXT RPG");
titleLabel.setForeground(Color.WHITE);
titleLabel.setFont(titleFont);
//Start Button Panel
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100 );
startButtonPanel.setBackground(Color.BLUE);
//Start Button
startButton = new JButton("START");
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
startButton.setFont(buttonFont);
//Add Elements to Window
titlePanel.add(titleLabel);
startButtonPanel.add(startButton);
//Add Elements to Container
c.add(titlePanel);
c.add(startButtonPanel);
}
}

Do not follow tutorial teaching you to use null layout managers and setting bounds "manually". That is not a good practice.
Remove all bounds setting from the code.
Instead use Layout Managers, that is what they do, dynamically set bounds for you:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Game {
JFrame window;
Container c;
JPanel titlePanel;
JPanel startButtonPanel;
JLabel titleLabel;
JButton startButton;
Font titleFont = new Font("Cooper Black", Font.PLAIN, 90);
Font buttonFont = new Font("Cooper Black", Font.PLAIN, 32);
public static void main(String[] args) {
new Game();
}
public Game() {
//Main Window
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = window.getContentPane();
c.setBackground(Color.BLACK);
//window.setLayout(null);
//Title Panel
titlePanel = new JPanel(); //JPanel uses FlowLayout by default
//titlePanel.setBounds(100, 100, 600, 150)
titlePanel.setBackground(Color.GRAY);
titleLabel = new JLabel("TEXT RPG");
titleLabel.setForeground(Color.WHITE);
titleLabel.setFont(titleFont);
//Start Button Panel
startButtonPanel = new JPanel();
//startButtonPanel.setBounds(300, 400, 200, 100 );
startButtonPanel.setBackground(Color.BLUE);
//Start Button
startButton = new JButton("START");
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
startButton.setFont(buttonFont);
//Add Elements to Window
titlePanel.add(titleLabel);
startButtonPanel.add(startButton);
//Add Elements to Container
c.add(titlePanel, BorderLayout.CENTER); //JFrame content pane uses BorderLayout by default
c.add(startButtonPanel, BorderLayout.PAGE_END);
window.pack();
window.setVisible(true); //invoke after all added and pack() ed
}
}
Don't expect to get the exact desired look on first attempt. Learn how to use different Layout Managers and combinations of them to get what you want.

A friend of mine offfered to fix the problem. He added something called "extends Canvas".
Heres the fixed version:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Game extends Canvas{
private static final long serialVersionUID = 1L;
public static JFrame window;
public static Container c;
JPanel titlePanel,startButtonPanel;
JLabel titleLabel;
JButton startButton;
Font titleFont = new Font("Cooper Black", Font.PLAIN,90);
private static int width = 800;
private static int height = 600;
public static String title ="Blueberry's Game";
/*----------------------------------------------------------------------------------------------------*/
public static void main(String[] args){
Game game = new Game();
Game.window.setResizable(false);
Game.window.setTitle(Game.title);
Game.window.add(game);
Game.window.pack();
Game.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game.window.setLocationRelativeTo(null);
Game.window.setVisible(true);
}
/*----------------------------------------------------------------------------------------------------*/
public Game(){
window = new JFrame();
setPreferredSize(new Dimension(width, height));
window.getContentPane().setBackground(Color.BLACK);
c = window.getContentPane();
//////////TITLE PANEL//////////////////////
titlePanel = new JPanel();
titlePanel.setBounds(100, 100, 600, 150);
titlePanel.setBackground(Color.BLACK);
titleLabel = new JLabel("TEXT RPG");
titleLabel.setForeground(Color.WHITE);
titleLabel.setFont(titleFont);
//////////START BUTTON PANEL//////////////////////
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setForeground(Color.BLUE);
//////////START BUTTON//////////////////////
startButton = new JButton("START");
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
//////////ADD ELEMENTS TO WINDOW//////////////////////
titlePanel.add(titleLabel);
startButtonPanel.add(startButton);
//////////ADD ELEMENTS TO CONTAINER//////////////////////
c.add(titlePanel);
c.add(startButtonPanel);
}
/*----------------------------------------------------------------------------------------------------*/
}
EDIT: I Also managed to see why the window wasn't showing anything in my question. I just had to adjust the window size with the cursor.

Related

Adding GUI to SOAPui to insert variables

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.*
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import com.eviware.soapui.support.types.StringToStringMap
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Swing05 extends WindowAdapter
{
public Swing05()
{
Vector vect02 = new Vector();
vect02.addElement("ACTIVE");
vect02.addElement("VERIFY");
vect02.addElement("PENDING");
vect02.addElement("DEPLOYMENT");
vect02.addElement("PLANNING");
JComboBox cmboBox01 = new JComboBox(vect02);
cmboBox01.setAlignmentY(Component.TOP_ALIGNMENT);
// PANEL01 - VERTICAL LAYOUT BUTTONS
JTextField button02 = new JTextField("Cookie");
JTextArea button03 = new JTextArea("Locked By User");
button03.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel panel01 = new JPanel();
panel01.setLayout(new BoxLayout(panel01,BoxLayout.Y_AXIS));
panel01.add(button02);
panel01.add(Box.createVerticalStrut(5));
panel01.add(button03);
panel01.add(Box.createVerticalStrut(5));
panel01.add(cmboBox01);
//panel01.add(Box.createVerticalGlue());
//panel01.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel right_panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
//right_panel.setBorder(BorderFactory.createLineBorder(Color.black));
right_panel.add(panel01);
// PANEL02 - HORIZONTAL LIST AND PANEL01
// Here, we are going to create a list of elements and place them into a BorderLayout panel
// in the centre to ensure that the list will expand in size relative to the window size.
Vector vect01 = new Vector();
vect01.addElement("COUNTRY_STG");
vect01.addElement("LOB_STG");
vect01.addElement("MRT_LABOR_BASE_RATES_STG");
vect01.addElement("MRT_LABOR_ADDERS_STG");
vect01.addElement("INFLATION_STG");
vect01.addElement("MRT_PRICE_CONFIG");
vect01.addElement("MRT_WW_CONFIG");
vect01.addElement("PAYMENT_TERMS");
vect01.addElement("PAYMENT_TERMS_MAPPING");
vect01.addElement("COUNTRY_CURR_CONFIG");
vect01.addElement("SDM_OFFERING_HIERARCHY");
vect01.addElement("SDM_FWB_MAPPING");
JList list01 = new JList(vect01);
JScrollPane scrPane = new JScrollPane(list01);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
// p1.setBorder(BorderFactory.createEtchedBorder());
p1.add(scrPane,BorderLayout.CENTER);
JPanel panel02 = new JPanel();
panel02.setLayout(new BoxLayout(panel02,BoxLayout.X_AXIS));
panel02.add(Box.createHorizontalStrut(5));
panel02.add(p1);
panel02.add(Box.createHorizontalStrut(5));
//panel02.add(panel01);
panel02.add(right_panel);
panel02.add(Box.createHorizontalStrut(5));
// PANEL03 - HORIZONTAL LAYOUT FOR BUTTONS
JButton button05 = new JButton("Close");
JButton button06 = new JButton("Apply");
JPanel panel03 = new JPanel();
panel03.setLayout(new BoxLayout(panel03,BoxLayout.X_AXIS));
panel03.add(Box.createHorizontalGlue());
panel03.add(Box.createHorizontalStrut(5));
panel03.add(button05);
panel03.add(Box.createHorizontalStrut(5));
panel03.add(button06);
panel03.add(Box.createHorizontalStrut(5));
// PANEL04 - HOLDS PANEL02 and PANEL03
JPanel panel04 = new JPanel();
panel04.setLayout(new BoxLayout(panel04,BoxLayout.Y_AXIS));
panel04.add(Box.createVerticalStrut(5));
panel04.add(panel02);
panel04.add(Box.createVerticalStrut(0));
panel04.add(panel03);
panel04.add(Box.createVerticalStrut(5));
// Put EVERYTHING into a scrollable pane so if the frame isn't large enough to
// display everything, it can be reached by scrolling the pane.
JScrollPane mainPane = new JScrollPane(panel04);
// CREATE THE WINDOW AND DISPLAY THE FRAME
JFrame frame01 = new JFrame();
frame01.setContentPane(mainPane);
frame01.setTitle("Insert Variables");
frame01.setSize(500,200);
frame01.setLocation(200,200);
frame01.addWindowListener(this);
frame01.pack();
frame01.setVisible(true);
frame01.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
button05.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frame01.dispose();
}
});
}
public static void main(String [] args)
{
Swing05 app01 = new Swing05();
}
}
Im trying to build an interface to select some variables for SoapUI .
im failing miserably .
can someone point me in the right direction ?
i wasnt able connecting the selected items to variables that can be used further.
I believe you would like to add GUI support to SoapUI through Groovy scripts. In this case I would suggest to opt for plugin development for SoapUI with GUI features.
Here is the sample which you can adapt - https://github.com/olensmar/soapui-emailtestsstep-plugin/blob/master/src/main/java/soapui/demo/teststeps/email/EMailTestStepDesktopPanel.java

repaint() not invoking paintComponent()?

Hello I am trying to solve the following problem: Write a program that prompts the user to enter the x- and y-positions of a center point and a radius, using text fields. When the user clicks a "Draw" button, draw a circle with that center and radius in a component. I do not see what is wrong in my code but something is because it doesnt seem like repaint() is invoking paintComponent() as message will change to TESTING 1 but not TESTING 2 and no drawing is made.
My Code:
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class q3{
public static class cgPanel extends JPanel{
private static double x;
private static double y;
private static double r;
private static JTextField xField;
private static JTextField yField;
private static JTextField rField;
private static JButton draw;
private static JLabel message;
//This is all just Layout work.
public cgPanel(){
setLayout(new BorderLayout());
JPanel drawPanel = new JPanel();
drawPanel.setBackground(Color.WHITE);
add(drawPanel, BorderLayout.CENTER);
message = new JLabel("");
JPanel sub1ForSub1 = new JPanel();
sub1ForSub1.add(message);
JLabel coordinates = new JLabel("Coordinates:");
JPanel sub2ForSub1 = new JPanel();
sub2ForSub1.add(coordinates);
JPanel subPanel1 = new JPanel();
subPanel1.setLayout(new GridLayout(2, 1));
subPanel1.add(sub1ForSub1);
subPanel1.add(sub2ForSub1);
JLabel xLabel = new JLabel("x:");
xField = new JTextField(4);
JLabel yLabel = new JLabel(" y:");
yField = new JTextField(4);
JLabel rLabel = new JLabel(" Radius:");
rField = new JTextField(4);
JPanel subPanel2 = new JPanel();
subPanel2.add(xLabel);
subPanel2.add(xField);
subPanel2.add(yLabel);
subPanel2.add(yField);
subPanel2.add(rLabel);
subPanel2.add(rField);
draw = new JButton("Draw");
ActionListener bL = new ButtonListener();
draw.addActionListener(bL);
JPanel subPanel3 = new JPanel();
subPanel3.add(draw);
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
Panel.add(subPanel1, BorderLayout.NORTH);
Panel.add(subPanel2, BorderLayout.CENTER);
Panel.add(subPanel3, BorderLayout.SOUTH);
add(Panel, BorderLayout.SOUTH);
setVisible(true);
}
static class ButtonListener extends JComponent implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
String xString = xField.getText();
String yString = yField.getText();
String rString = rField.getText();
message.setText("TESTING 1");
x = Double.parseDouble(xString);
y = Double.parseDouble(yString);
r = Double.parseDouble(rString);
repaint();
}
catch (NumberFormatException exception){
message.setText("Please enter a number.");
}
}
//This is where I cant seem to get the code in paintComponent to run when the Draw button is pressed.
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(x - r, y - r, r*2, r*2);
g2.draw(circle);
message.setText("TESTING 2");
}
}
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(800, 800);
frame.setTitle("Circle Generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cgPanel panel = new cgPanel();
frame.add(panel);
frame.setVisible(true);
}
}
So, a couple of things.
Your problem stems from that fact that your ButtonListener is extending a JComponent, so the repaint() method is calling the one for the ButtonListener (which really isn't a JComponent).
And the paintComponent method is also for the the ButtonListener.
Instead, you want your button listener to have access to your cgPanel, so it can tell IT to repaint. And your paintComponent needs to be moved to the cgPanel, but even then you probably don't want it there since you have a bunch of other components on cgPanel.
It's not clear from your code where you really want the circle to be drawn.
You should probably create a CirclePanel that extend JPanel, and overrides paintComponent to draw your circles, and then add that to your cgPanel. Then make your ButtonListener tell the CirclPanel instance to repaint.

JSlider triangle is translucent with MetalUI

I'm on Mac OSX 10.7 64 Bit.
I want a JSlider using MetalUI.
package test;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class Test {
public static void main(String args[]) throws UnsupportedLookAndFeelException {
JFrame frame = new JFrame("Tick Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JSlider slider = new JSlider(-100, 100, 0);
LookAndFeel save = UIManager.getLookAndFeel();
LookAndFeel laf = new MetalLookAndFeel();
UIManager.setLookAndFeel(laf);
slider.setUI(new javax.swing.plaf.metal.MetalSliderUI());
frame.add(slider, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
UIManager.setLookAndFeel(save);
}
}
This example program shows me the JSlider but the triangle is translucent and I can see the rendered value bar. How can I fix that?

how to display the contents of a tab in the workspace of Jtabbedpane?

i'v put a jpanel on the tab of Jtabbedpane which is at the left side ,nw when the tab is clicked i want to display labels and textfields in the workspace of the tabbedpane,plz hlp.
When you say 'workspace of the tabbedpane' I think you are referring to your JPanel?
You must attach the JLabels and JTextfields to the JPanel using JPanel.add() before you add the JPanel to the JTabbedPane.
I think you should do exactly as #7SpecialGems says (+1 for that).
Here is an example showing how exactly it can look like in code.
import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JTabbedPaneSample
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
createGUI();
}
});
}
private static void createGUI() throws HeadlessException
{
JPanel oi1 = new JPanel();
oi1.setBackground(Color.RED);
oi1.add(new JLabel("Label 1"));
JPanel oi2 = new JPanel();
oi2.setBackground(Color.PINK);
oi2.add(new JLabel("Label 2"));
oi2.add(new JTextField("TextField 1"));
JTabbedPane tp = new JTabbedPane();
tp.addTab("Oi1", oi1);
tp.addTab("Oi2", oi2);
JPanel contentPane = new JPanel();
contentPane.add(tp);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Good luck, Boro.

JSplitPane + MiGLayout: how to enable autoresizing

I'm doing something wrong here: I want to have two JButtons in a JSplitPane in a JPanel in a a JFrame, where the buttons fill the JSplitPane.
Here's what I get when I resize the JFrame:
The buttons stay their normal size, and the JSplitPane doesn't allow adjusting.something
How do I fix this?
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import net.miginfocom.swing.MigLayout;
public class SplitPaneQuestion {
public static void main(String[] args) {
JFrame frame = new JFrame("SplitPaneQuestion");
JPanel panel = new JPanel();
frame.setContentPane(panel);
panel.setLayout(new MigLayout("","[]","[grow]"));
JSplitPane splitpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
panel.add(splitpane, "");
splitpane.setTopComponent(new JButton("top"));
splitpane.setBottomComponent(new JButton("bottom"));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Add the "push" and "grow" contraints to your split pane, like this:
panel.add(splitpane, "push, grow");