public class StartMenu extends JFrame {
public static void StartMenu() {
JFrame StartMenu = new JFrame("SamBallPool"); //New Windwow With Title
StartMenu.add(new JLabel(new ImageIcon("Data/StartMenu.png")));
StartMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set Close Operation
StartMenu.setSize(600, 330);
StartMenu.setVisible(true);
JPanel StartPanel = new JPanel();
StartPanel.setLayout(null);
StartPanel.setVisible(true);
ImageIcon Start = new ImageIcon("Data/StartButton.png"); //Play Button in Center
JButton StartButton = new JButton(Start);
StartButton.setBounds(263,101,70,53); //Positioned Button Over Image of Button
StartButton.setVisible(true);
StartButton.setEnabled(true);
StartButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent StartButtonClicked){
JOptionPane.showMessageDialog(null,"Test");
MainMenu.MainMenu();
StartMenu.dispose();
}
});
StartPanel.add(StartButton);
StartMenu.add(StartPanel);
}
}
The button does not show up, filepath is correct, even without the image it doesn't show up. It used to show up but stopped working for some reason, thanks for help in advance :(
You call StartPanel.setVisible(true); too early. It must be the last statement after configuration of the form is completed.
Related
I'm working on an assignment for class where we need to create a JComboBox and each option opens a new window where you can do whatever you want on those new windows. Please keep in mind I'm very new to GUI and new to Java in general, in case my questions are dumb.
I have a question and an issue...
My question:
When the user selects "The Matrix" option a new window pops up with a quote and two buttons. Right now I have two JPanels (panel and panel2) panel adds the quote to the NORTH position and then panel2 adds the two buttons to the CENTER position both using BorderLayout. My question is am I doing this correctly...Could I use just panel to add the quote and the buttons or is it necessary to create separate panels for separate items being added to the JFrame? When I had them both added to the same panel the quote was not on the window when I ran the program.
panel.add(matrixQuote);
newFrame.add(panel, BorderLayout.NORTH);
That's how I had it when it wasn't showing up ^^^
I GOT THE ISSUE WITH CLEARING THE JFRAME FIXED
I am trying to add an ActionListener to the bluePill button and instead of opening another new window I thought I could clear everything from the existing window when the button is pressed and then display something new on said window. The only info I could find on this is how I have it in the actionPerformed method below. I'll post a snippet of what I'm talking about directly below and then all my code below that just in case.
All my code...
public class MultiForm extends JFrame{
private JComboBox menu;
private JButton bluePill;
private JButton redPill;
private JLabel matrixQuote;
private int matrixSelection;
private JFrame newFrame;
private JPanel panel;
private JPanel panel2;
private static String[] fileName = {"", "The Matrix", "Another Option"};
public MultiForm() {
super("Multi Form Program");
setLayout(new FlowLayout());
menu = new JComboBox(fileName);
add(menu);
TheHandler handler = new TheHandler();
menu.addItemListener(handler);
}
public void matrixPanel() {
TheHandler handler = new TheHandler();
//Create a new window when "The Matrix" is clicked in the JCB
newFrame = new JFrame();
panel = new JPanel();
panel2 = new JPanel();
newFrame.setLayout(new FlowLayout());
newFrame.setSize(500, 300);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
matrixQuote = new JLabel("<html>After this, there is no turning back. "
+ "<br>You take the blue pill—the story ends, you wake up "
+ "<br>in your bed and believe whatever you want to believe."
+ "<br>You take the red pill—you stay in Wonderland, and I show"
+ "<br>you how deep the rabbit hole goes. Remember: all I'm "
+ "<br>offering is the truth. Nothing more.</html>");
panel2.add(matrixQuote);
newFrame.add(panel2, BorderLayout.NORTH);
//Blue pill button and picture.
Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
bluePill = new JButton("Blue Pill", bp);
panel2.add(bluePill);
bluePill.addActionListener(handler);
//Red pill button and picture
Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
redPill = new JButton("Red Pill", rp);
panel2.add(redPill);
newFrame.add(panel2, BorderLayout.CENTER);
newFrame.setVisible(true);
}
private class TheHandler implements ItemListener, ActionListener{
public void itemStateChanged(ItemEvent IE) {
//listen for an item to be selected.
if(IE.getStateChange() == ItemEvent.SELECTED) {
Object selection = menu.getSelectedItem();
if("The Matrix".equals(selection)) {
matrixPanel();
}
else if("Another Option".equals(selection)) {
}
}
}
public void actionPerformed(ActionEvent AE) {
if(AE.getSource() == bluePill) {
newFrame.remove(panel);
newFrame.remove(panel2);
newFrame.repaint();
}
}
}
//MAIN
public static void main(String[] args) {
MultiForm go = new MultiForm();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 200);
go.setVisible(true);
}
}
Use a Card Layout. You can swap panels as required.
The tutorial has a working example.
You can use:
jpanel.removeAll();
Either to delete a certain JComponent by using the JComponent itself like:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
frame.remove(panel);
panel.getGraphics().clearRect(0, 0, panel.getWidth(), panel.getHeight());
I have my JButton set up and everything but it does absolutely nothing. Could someone tell me how to add a command such as system.out.println or some Scanner commands to a JButton?
Here is my line of code. It is very simple and I'm just testing JButton to add it to some of my other programs
import javax.swing.*;
public class Swing extends JFrame {
JButton load = new JButton("Load");
JButton save = new JButton("Save");
JButton unsubscribe = new JButton("Unsubscribe");
public ButtonFrame() {
super ("ButtonFrame");
setSize(140, 170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(load);
pane.add(save);
pane.add(unsubscribe);
add(pane);
setVisible(true);
}
public static void main(String[] arguments) {
ButtonFrame bf = new ButtonFrame();
}
}
See How to Write an Action Listener.
I suggest you read the entire tutorial (or keep a link to it for reference) as it contains all the Swing basics.
Hope this helps
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Here goes the action (method) you want to execute when clicked
System.out.println("You clicked the button load");
}
});
//The same for save button
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Here goes the action (method) you want to execute when clicked
System.out.println("You clicked the button save");
}
});
Hello I am writing a sort of Menu for an encryption program I am writing. I finished the core of it, and now i want to try to create a GUI for it. Here is the code for the first Menu:
package matrix_with_GUI;
import javax.swing.*;
import java.awt.event.* ;
import java.awt.* ;
public class Main_Menu extends JFrame implements ActionListener{
private JButton action1 = new JButton ("");
private JButton action2 = new JButton ("");
private JPanel pane = new JPanel();
private JLabel lbl;
public static void main(String[] args) {
Main_Menu main = new Main_Menu();
}
public Main_Menu(){
super();
JPanel pane=new JPanel();
setTitle ("Start Menu") ;
JFrame frame = new JFrame("");
setVisible(true);
setSize (380, 260) ;
setLocation (450, 200) ;
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
action1 = new JButton("Start");
action2 = new JButton("Exit");
lbl = new JLabel ("Welcome to the Matrix Encoder/Decoder!!!");
setLayout(new FlowLayout());
add (lbl) ;
add(action1, BorderLayout.CENTER);
action1.addActionListener (this);
add(action2, BorderLayout.CENTER);
action2.addActionListener (this);
}
#Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
OptionsMenu x = new OptionsMenu();
if (event.getSource() == action1)
{
System.exit(0);
x.OptionsMenu();
}
else if(event.getSource() == action2){
System.exit(0);
}
}
}
When the Start Button is clicked, the new menu comes up all fine and good, but the first menu stays open. Is there a way to close this first menu AND open the second menu with the first button click? I'm very new to GUI so the simplest solution would be very helpful. On a side note is there a simple way to move the Start button to the next line? Thanks
You have 2 options: you can use a Window Listener, or you can use the dispose() method. To do the dispose() just type
* This is better to be used with subframes and 2nd level windows.*
this.dispose();
or check this link for using the window listener
Closing JFrame
In order to close the Main_Menu, you can just call its dispose method:
this.dispose();
instead of calling System.exit(0), which will terminate the JVM altogether.
i create a dialog box in java desktop application . But when i hide/show label and button by applying condition on checkbox .its produce graphical noise by showing some part of background application part(like red box on both label and buttons also checkbox layout causes problem). i write this condition on checkbox.
checkbox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (checkbox.isSelected()) {
baisvalue.setVisible(true); //label
plusbais.setVisible(true); //button
minisbais.setVisible(true); //button
}
if (!checkbox.isSelected()) {
minisbais.setVisible(false); //label
plusbais.setVisible(false); //button
baisvalue.setVisible(false); //button
}
}
});
note:
i also call repaint(); & validate(); but same problem occurs.
I wasn't able to recreate your problem - I see no graphical noise. I've attached a sscce of what I tried - Can you reproduce your problem with this example? If so, can you provide us with more information about your java version/platform? If not, can you modify this example to recreate your problem (and edit your question with the code)?
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends Box{
JCheckBox checkbox = new JCheckBox("Select Me");
JLabel baisvalue = new JLabel("baisvalue");
JButton plusbais = new JButton("plusbais");
JButton minisbais = new JButton("minisbais");
public MainPanel(){
super(BoxLayout.Y_AXIS);
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (checkbox.isSelected()) {
baisvalue.setVisible(true); //label
plusbais.setVisible(true); //button
minisbais.setVisible(true); //button
}
if (!checkbox.isSelected()) {
minisbais.setVisible(false); //label
plusbais.setVisible(false); //button
baisvalue.setVisible(false); //button
}
}
};
checkbox.addActionListener(l);
add(checkbox);
add(baisvalue);
add(plusbais);
add(minisbais);
//Performs the action on initialization
l.actionPerformed(new ActionEvent(checkbox, ActionEvent.ACTION_PERFORMED, ""));
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Try to use this.setOpaque(false); in constructor.
Here is the code of my Layout class.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Layout extends JFrame {
private JButton lb;
private JButton cb;
private JButton pb;
private FlowLayout layout;
private Container container;
public Layout() {
super("The Title");
layout = new FlowLayout();
container = new Container();
setLayout(layout);
//*Left
lb = new JButton("L");
add(lb);
lb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(container);
}
}
);
//*Center
cb = new JButton("C");
add(cb);
cb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
}
);
//*Right
pb = new JButton("R");
add(pb);
pb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
}
);
}
}
I'm learning java through thenewboston youtube tutorials (this code is from this tutorial). But this one doesn't work like it should. When I click the right button (R) it should drag all the buttons to the right side of the window instantly. It doesnt. However when I click that right button and then forcibly resize the window then it do what it should. By adding setResizable(false) in the main method I cant resize the program so it doesnt work.
What have I done wrong?
Forgive me for my poor English btw.
replace
container = new Container();
by
container = getContentPane();
The simplest way would be to set a new FlowLayout and call invalidate():
setLayout(new FlowLayout(FlowLayout.LEFT));
invalidate();
validate();
Updating the current FlowLayout has no effect.