i have two button is btn_turn1 and btn_turn2 , i want when user click Enough btn_turn1 and btn_turn2 after event "if" will printf to screen at txt_turn but it just printf "0", help me
First of all JButton in swing can not be selected. it is a button, it can be clicked, entered, pressed, etc.
it is hard to understand what you are asking for, but i assume you are trying to count clicks from 2 Jbuttons and have to display it in Jtextfield. most simple and easy approach is next.
public class Cit extends JPanel implements ActionListener {
JButton btn_dahn1;
JButton btn_dahn2;
JTextField textField;
static int step ;
public Cit(){
btn_dahn1 = new JButton("Button1");
btn_dahn1.addActionListener(this);
btn_dahn2 = new JButton("Button2");
btn_dahn2.addActionListener(this);
textField = new JTextField();
btn_dahn1.setPreferredSize(new Dimension(100,30));
btn_dahn1.setMaximumSize(new Dimension(100,30));
btn_dahn2.setPreferredSize(new Dimension(100,30));
btn_dahn2.setMaximumSize(new Dimension(100,30));
textField.setMaximumSize(new Dimension(100,30));
textField.setMaximumSize(new Dimension(100,30));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setPreferredSize(new Dimension(300, 30));
add(btn_dahn1);
add(btn_dahn2);
add(textField);
}
you can add Cit class to frame or dialog or whatever you use, then click button1 and button2, jtextfield will display the steps.
if you are not familiar how to do that. look at next code part.
public static void main(String[] args) {
JFrame frame = new JFrame();
Cit cit = new Cit();
frame.getContentPane().add(cit);
frame.setVisible(true);
frame.pack();
}
I have read many subjects here but I can't make my window with the layout I want.
I simply want all my graphic object to be in a row style like in the first picture here : http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html
I've tried GridLayout but it still make my first button giant and then, as I add textfields, it's getting smaller and smaller?!
Here is my code without all the imports:
public class TestScrollPane extends JFrame implements ActionListener{
Dimension dim = new Dimension(200 , 50);
JButton button;
JPanel panel = new JPanel();
JScrollPane scrollpane = new JScrollPane(panel);
public TestScrollPane(){
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.add(scrollpane);
//panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
button = new JButton("click me");
button.setPreferredSize(dim);
panel.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == button ){
JTextField txt = new JTextField(); // we add a new button
txt.setPreferredSize(dim);
panel.add(txt);
SwingUtilities.updateComponentTreeUI(this); // refresh jframe
}
}
public static void main(String[] args){
TestScrollPane test = new TestScrollPane();
}
}
I just want to have one button per row.
A BoxLayout will respect the minimum/maximum sizes of a component.
For some reason the maximum height of a text field is unlimited so the text field gets all the space available.
So you can do something like:
JTextField txt = new JTextField(10); // we add a new button
//txt.setPreferredSize(dim); // don't hardcode a preferrd size of a component.
txt.setMaximumSize(txt.getPreferredSize());
Also:
//SwingUtilities.updateComponentTreeUI(this); // refresh jframe
Don't use the above method. That is used for a LAF change.
Instead when you add/remove components from a visible GUI you should use:
panel.revalidate();
panel.repaint();
Okay, so I'm having some trouble with my Programming Exercise today.
The Exercise text goes like this:
(Use the FlowLayout manager) Write a program that meets the following requirements:
Create a frame and set its layout to FlowLayout
Create two panels and add them to the frame
Each panel contains three buttons. The panel uses FlowLayout
The buttons should be named "Button 1", "Button 2" and so on.
I think I'm having some trouble with adding the panels to the frame because when i run the program, it shows an empty frame.
Here is the code i have.
import javax.swing.*;
import java.awt.*;
public class Exercise12_1 extends JFrame {
public Exercise12_1() {
setLayout(new FlowLayout());
JFrame frame = new JFrame(" Exercise 12_1 ");
frame.setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
frame.add(panel1);
frame.add(panel2);
}
public static void main(String[] args) {
Exercise12_1 frame = new Exercise12_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I would greatly appreciate it if some of you took your time to help me out here.
Thanks.
Your main method creates a frame:
Exercise12_1 frame = new Exercise12_1();
and then makes it visible.
And the constructor of this 'Exercise12_1' frame creates another frame, and adds panel to this other frame:
JFrame frame = new JFrame(" Exercise 12_1 ");
frame.setLayout(new FlowLayout());
The constructor shouldn't create another frame. It should add the panels to this: the frame being constructed, and that will then be made visible.
Also, you should not use setSize(), but pack(), to make the frame have the most appropriate size based on the preferred size of all the components it contains.
Check this:
import javax.swing.*;
import java.awt.*;
public class Exercise12_1 extends JFrame {
public Exercise12_1() {
setLayout(new FlowLayout());
//JFrame frame = new JFrame(" Exercise 12_1 ");
this.setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
this.add(panel1);
this.add(panel2);
}
public static void main(String[] args) {
Exercise12_1 frame = new Exercise12_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You use use such code like this.getContentPane().add(panel1); to add Panel.
Change
frame.add(panel1);
frame.add(panel2);
To
this.getContentPane().add(panel1);
this.getContentPane().add(panel2);
and it will be working then.
This is my first time posting here.
I am using GUI in java for the very first time while working on some homework. I have started incrementally coding a Italian restaurant menu.
The below code is compiling fine with no errors. After compiling I run applet viewer Italian.html and the applet viewer screen only displays a blank window. I am a little confused as I have no errors to work with. Am I missing something simple.
Thanks for any help.
import javax.swing.*;
import java.awt.*;
public class Italian extends JApplet {
//Declare and array for a list of Pastas
private String [] pastas = {"Spaghetti", "Angel Hair Pasta", "Tortellini",
"Ziti"};
private String [] sauces = {"Maranaria", "Alfredo", "Spicy Marania"};
public Italian() {
//Create the base panel for the restaurant page
JPanel i1 = new JPanel();
i1.setLayout(new GridLayout(2, 1));
i1.add(new JComboBox(pastas));
i1.add(new JComboBox(sauces));
HTML
<html>
<head>
<title>Java Applet Demo</title>
</head>
<body>
<applet
code = "Italian.class"
width = 250
height = 250>
</applet>
</body>
</html>
You haven't added anything to the applet for the applet to show on the screen.
Either in your constructor or you init method you need to add the panel you've created to the content pane.
getContentPane().setLayout(new BorderLayout()); // Just to make sure
getContentPane().add(i1);
you shoud add something to applet to make them work.here is a link
http://math.hws.edu/eck/cs124/javanotes4/c6/index.html which will help you to know applets and graphics.
this eg. will help you which adds different buttons with proper lay out:
/*
This applet demonstrates various layout managers.
The applet itself uses a border layout with a JPanel in
the center, a JComboBox menu to the North, and a JLabel
to the south. The center panel uses a CardLayout.
Each card in the card layout contains a number of
buttons and uses a different layout manager. The
JComboBox menu is used to select among these cards.
The JLabel reports events as they occur.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutDemo extends JApplet
implements ActionListener, ItemListener {
CardLayout cards; // the layout manager for the center panel
JPanel cardPanel; // the center panel
JComboBox panelChoice; // menu for selecting which card to show
JLabel message; // a message shown at the bottom of the applet
public void init() {
panelChoice = new JComboBox(); // Set up the menu
panelChoice.setBackground(Color.white);
panelChoice.addItem("FlowLayout"); // Add in the names of the cards.
panelChoice.addItem("FlowLayout with Big Hgap");
panelChoice.addItem("Vertical BoxLayout");
panelChoice.addItem("Horizontal BoxLayout with Struts");
panelChoice.addItem("BorderLayout");
panelChoice.addItem("GridLayout(3,2)");
panelChoice.addItem("GridLayout(1,0)");
panelChoice.addItem("GridLayout(0,1)");
panelChoice.addItemListener(this);
message = new JLabel("Layout Demo", JLabel.CENTER); // Set up the mesage
message.setBackground(Color.white);
message.setOpaque(true); // so background color will show
message.setBorder(BorderFactory.createEmptyBorder(5,0,3,0));
message.setForeground(Color.red);
cardPanel = new JPanel(); // Set up the center panel
cardPanel.setBackground(Color.white);
cards = new CardLayout();
cardPanel.setLayout(cards);
setBackground(Color.blue);
getContentPane().setBackground(Color.blue);
getContentPane().setLayout(new BorderLayout(3,3));
getContentPane().add("Center",cardPanel);
getContentPane().add("North",panelChoice);
getContentPane().add("South",message);
JPanel panel; // Will represent various cards to be added to the center panel.
Box box; // For the cards that use a BoxLayout.
// Set up each "card" in the center panel to have its own layout
// manager and to contain a variety of buttons.
panel = new JPanel();
// use default FlowLayout for panel
panel.setBackground(Color.white);
cardPanel.add(panel, "FlowLayout");
addButton(panel,"First Button"); // ( addButton is a untility method, defined below )
addButton(panel,"Second Button");
addButton(panel,"Third Button");
addButton(panel,"Fourth Button");
addButton(panel,"Fifth Button");
addButton(panel,"Sixth Button");
addButton(panel,"Seventh Button");
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER,30000,5));
panel.setBackground(Color.white);
cardPanel.add(panel,"FlowLayout with Big Hgap");
addButton(panel," A Button");
addButton(panel,"Another Button");
addButton(panel,"A Third Button");
addButton(panel,"A Fourth Button");
addButton(panel,"A Final Button");
box = Box.createVerticalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Vertical BoxLayout");
addButton(box,"Button One");
addButton(box,"Button Two");
addButton(box,"Button Three");
addButton(box,"Button Four");
addButton(box,"Button Five");
addButton(box,"Button Six");
box = Box.createHorizontalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Horizontal BoxLayout with Struts");
addButton(box,"1st");
addButton(box,"2nd");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"3rd");
addButton(box,"4th");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"5th");
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
cardPanel.add(panel,"BorderLayout");
addButton(panel,"Center Button", BorderLayout.CENTER);
addButton(panel,"North Button", BorderLayout.NORTH);
addButton(panel,"South Button", BorderLayout.SOUTH);
addButton(panel,"East Button", BorderLayout.EAST);
addButton(panel,"West Button", BorderLayout.WEST);
panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(3,2)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
panel = new JPanel();
panel.setLayout(new GridLayout(1,0));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(1,0)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(0,1)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
} // end init()
public Insets getInsets() {
// specify borders around the edges of the applet
return new Insets(3,3,3,3);
}
void addButton(Container p, String name) {
// Create a button with the given name and add it
// to the given panel. Set up the button to send
// events to the applet.
JButton b = new JButton(name);
p.add(b);
b.addActionListener(this);
}
void addButton(JPanel p, String name, Object option) {
// Same as above, but use the "option" object
// as an additional parameter in the add method.
JButton b = new JButton(name);
p.add(b, option);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
// A button was pressed. Report the name
// of the button by setting the message text.
String buttonName = evt.getActionCommand();
message.setText("Button \"" + buttonName + "\" was pressed.");
}
public void itemStateChanged(ItemEvent evt) {
// The user has selected an item from the JComboBox.
// Change the displayed card to match.
String panelName = (String)panelChoice.getSelectedItem();
cards.show(cardPanel, panelName);
message.setText("Panel \"" + panelName + "\" was selected.");
}
} // end class LayoutDemo
Here is a screenshot of my JFrame. This will be the main window to my application.
So the problem is that all the buttons are inline with each other, whereas I want them to be one under the other i.e. Add Contact under Show Contacts.
So how can I do that?
Here is my code for the JFrame.
public class CRUDFrame extends JFrame {
public CRUDFrame(){
super("AppCRUD");
setLayout(new FlowLayout());
JButton button1, button2, button3, button4;
button1 = new JButton(" Show Contacts ");
button2 = new JButton(" Add Contact ");
button3 = new JButton(" Update Number in a Contact ");
button4 = new JButton(" Delete a Contact ");
add(button1);
add(button2);
add(button3);
add(button4);
}
}
`
There have been some good answers centered around 'use a layout'. This example espouses the same advice, but also introduces the concept of nesting one layout within another. E.G. the JPanel containing the JButtons has a GridLayout. That panel is placed in the NORTH of a panel that is then added to the WEST of the main 'gui' panel.
The other components are added in order to show how the column of buttons might go together with other components in the main user interface.
Contact.java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class Contact {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new EmptyBorder(3,3,3,3) );
JPanel controls = new JPanel( new BorderLayout(5,5) );
JPanel buttons = new JPanel(new GridLayout(0,1,4,4));
buttons.add( new JButton("Show") );
buttons.add( new JButton("Add") );
buttons.add( new JButton("Update Number") );
buttons.add( new JButton("Delete") );
buttons.setBorder( new TitledBorder("Contact") );
controls.add( buttons, BorderLayout.NORTH );
controls.add(new JScrollPane(new JTree()), BorderLayout.CENTER);
gui.add(controls, BorderLayout.WEST);
gui.add(new JTextArea("CardLayout for CRUD components.",10,30));
gui.add(new JLabel("Output label.."), BorderLayout.SOUTH);
JToolBar toolbar = new JToolBar();
toolbar.add(new JCheckBox("Auto save", true));
toolbar.add(new JCheckBox("Always On Top"));
gui.add(toolbar, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
Screenshot
Use box layout and set dimension for each button. Check the below link.
http://download.oracle.com/javase/tutorial/uiswing/layout/box.html
Take a look at http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html which discusses the various layout managers that Swing offers. GridBagLayout is probably what you need.