How to add a button to a JFrame Gui - java

I'm trying to add a button to a frame gui.
i tried making a panel and adding it to that, but it does not work.
please help!
here is my code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button;
JPanel panel;
// my error lines are under the "panel" and "button"
// it says i must implement the variables. what does that mean???
panel.add(button);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}

Change:
JButton button;
JPanel panel;
to:
JButton button = new JButton();
JPanel panel = new JPanel();
You can also pass a String value in JButton() constructor for that string value to be shown on the JButton.
Example: JButton button = new JButton("I am a JButton");

Example Code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
// Create JButton and JPanel
JButton button = new JButton("Click here!");
JPanel panel = new JPanel();
// Add button to JPanel
panel.add(button);
// And JPanel needs to be added to the JFrame itself!
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Output:
Note:
Create the JButton and JPanel using new JButton("..."); and new JPanel()
Add the JPanel to the JFrame's content pane using getContentPane().add(...);

If you can Change this Program, You can adjust the button place also
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class agui extends JFrame
{
agui()
{
setTitle("My GUI");
setSize(400,400);
setLayout(null);
JButton button = new JButton("Click Here..!");
button.setBounds(50,100,100,50); /*Distance from left,
Distance from top,length of button, height of button*/
add(button);
setVisible(true);
}
public static void main(String[] args)
{
JFrame agui = new agui();
}
}
Output

Related

Java GUI - buttons will not add to panel

Hello I am having a problem adding buttons to my GUI, I try to use BorderLayout to add the buttons but it does not show when I run. Since using BorderLayout my choice of background color reverts to white as well. Can anyone please help?
import javax.swing.*;
import java.awt.*;
public class BlackjackGUI{
private JFrame frame;
private JPanel panel;
private JButton newGameBtn, dealBtn, hitBtn, standBtn;
private JLabel playerMoneyLbl;
private JLabel playerCard1Lbl, playerCard2Lbl, playerCard3Lbl,
playerCard4Lbl, playerCard5Lbl, playerCard6Lbl, playerCard7Lbl;
private JLabel dealerCard1Lbl, dealerCard2Lbl, dealerCard3Lbl, dealerCard4Lbl,
dealerCard5Lbl, dealerCard6Lbl, dealerCard7Lbl;
private JLabel playerCardValueLbl, dealerCardValueLbl;
private JTextField betInputBox;
public BlackjackGUI(){
createForm();
addButtons();
frame.add(panel);
frame.setVisible(true);
}
public void createForm() {
JFrame frame = new JFrame("Blackjack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200,800);
frame.setVisible(true);
JPanel panel = new JPanel();
Color c = new Color(0, 100, 0);
panel.setBackground(c);
panel.setLayout(new BorderLayout());
}
public void addButtons() {
newGameBtn = new JButton("New Game");
panel.add(newGameBtn, BorderLayout.NORTH);
}
public static void main(String[] args) {
new BlackjackGUI();
}
}

how to align buttons in the middle java

I am a beginner in Java, but somehow have a bit of knowledge but still beyond. I wanted to ask , how do align buttons in this main menu I just created. The buttons are somehow aligned horizontally.
This is my code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mainmenu extends JFrame
{
JButton b1;
JLabel l1;
JButton b2;
public mainmenu() {
setResizable(false);
setLocation(100, 100);
setSize(500, 500);
setVisible(true);
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Master Boat\\Desktop\\PH\\BG HOROR.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
b2=new JButton(" EXIT! ");
b1.addActionListener(new btnFunc());
background.add(l1);
background.add(b1);
background.add(b2);
}
public void armaged() {
add(new gamesamplingbeta());
}
public static void main(String args[])
{
new mainmenu();
}
public class btnFunc implements ActionListener {
public void actionPerformed (ActionEvent e) {
}
}
public class btnFunc2 implements ActionListener {
public void actionPerformed2 (ActionEvent e) {
System.exit(1);
}
}
}
You should take a look at Swing Layouts for a whole bunch of different layout managers that allow you to position your components in a bunch of different ways.
The one I believe you should be using for this question is the Box Layout if you would like your buttons centered vertically.
Here is an example of one.
import javax.swing.*;
import java.awt.*;
public class MainFrame
{
JFrame mainFrame = new JFrame("Main Frame");
JPanel mainPanel = new JPanel();
JLabel label1 = new JLabel("Vertical Buttons");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
public MainFrame()
{
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(label1);
mainPanel.add(button1);
mainPanel.add(button2);
mainFrame.add(mainPanel);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(MainFrame::new);
}
}
If you would like to add spacing in between any of the components, use a Rigid Area like so
container.add(component);
container.add(Box.createRigidArea(new Dimension(100, 100));
container.add(component1);

How to open another frame in main frame?

I've created two jframes main_frame and sub_frame where main_frame holds a jbutton. Now i want that button to open sub_frame in the same frame(main_frame) and set main_frame disable until sub_frame is opened.
Note that I dont want main_frame to setVisible(false).
I suggest you use a CardLayout
Instead of multiple JFrames, you have multiple JPanels and switch between them.
Here is an example:
package main.frames;
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;
public MainFrame()
{
JButton showOtherPanelBtn = new JButton("Show Other Panel");
JButton backToHomeBtn = new JButton("Show Home Panel");
cl = new CardLayout(5, 5);
homeContainer = new JPanel(cl);
homeContainer.setBackground(Color.black);
homePanel = new JPanel();
homePanel.setBackground(Color.blue);
homePanel.add(showOtherPanelBtn);
homeContainer.add(homePanel, "Home");
otherPanel = new JPanel();
otherPanel.setBackground(Color.green);
otherPanel.add(backToHomeBtn);
homeContainer.add(otherPanel, "Other Panel");
showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));
add(homeContainer);
cl.show(homeContainer, "Home");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setTitle("CardLayout Example");
pack();
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(MainFrame::new);
}
}
It is really easy, just call the constructor and set visibility:
SubFrameClass frame = new SubFrameClass();
frame.setVisible(true);

The JLabel text is not changing on the new frame which is opened after clicking a button on the main frame

I have a button on frame1, clicking on it opens frame2 which has a button and a label. When I click button on frame2, the JLabel text is not changed. I want to change the JLabel text on frame2 when the button present on frame2 is clicked.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui{
JFrame frame1, frame2;
JPanel panel1, panel2;
JButton firstButton, secondButton;
JLabel prompt;
public static void main(String[] args){
Gui myGui = new Gui();
myGui.ui1();
}
public void ui1(){
frame1 = new JFrame();
frame1.setSize(500,500);
frame1.setTitle("First Frame");
panel1 = new JPanel();
firstButton = new JButton("click me");
frame1.add(panel1);
panel1.add(firstButton);
ListenForButtonOnUi1 buttonListener = new ListenForButtonOnUi1();
firstButton.addActionListener(buttonListener);
frame1.setVisible(true);
}
public void ui2(){
frame2 = new JFrame();
frame2.setSize(300,300);
frame2.setTitle("hell yeah");
panel2 = new JPanel();
secondButton = new JButton("done");
prompt = new JLabel("please fill in the details");
frame2.add(panel2);
panel2.add(prompt)
panel2.add(secondButton);
frame2.setVisible(true);
ListenForButtonOnUi2 buttonListener = new ListenForButtonOnUi2();
secondButton.addActionListener(buttonListener);
}
private class ListenForButtonOnUi1 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == firstButton){
Gui myGui = new Gui();
myGui.ui2();
}
}
}
private class ListenForButtonOnUi2 implements ActionListener{
public void actionPerforme(ActionEvent e){
if(e.getSource() == secondButton){
prompt.setText("text changed");
}
}
}
}

Swing JLabel text change on the running application

I have a Swing window which contains a button a text box and a JLabel named as flag. According to the input after I click the button, the label should change from flag to some value.
How to achieve this in the same window?
Use setText(str) method of JLabel to dynamically change text displayed. In actionPerform of button write this:
jLabel.setText("new Value");
A simple demo code will be:
JFrame frame = new JFrame("Demo");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(250,100);
final JLabel label = new JLabel("flag");
JButton button = new JButton("Change flag");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
label.setText("new value");
}
});
frame.add(label, BorderLayout.NORTH);
frame.add(button, BorderLayout.CENTER);
frame.setVisible(true);
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener
{
private JLabel label;
private JTextField field;
public Test()
{
super("The title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400, 90));
((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) );
setLayout(new FlowLayout());
JButton btn = new JButton("Change");
btn.setActionCommand("myButton");
btn.addActionListener(this);
label = new JLabel("flag");
field = new JTextField(5);
add(field);
add(btn);
add(label);
pack();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("myButton"))
{
label.setText(field.getText());
}
}
public static void main(String[] args)
{
new Test();
}
}

Categories

Resources