I am trying to generate a GUI using swing that creates a frame with 100 buttons and every button has a "label" on it from on 1 to a 100.
What I have tried:
import javax.swing.*;
import java.awt.*;
public class ButtonScreen extends JFrame{
JFrame frame = new JFrame();
int rows=10;
int cols=10;
JButton[][] button = new JButton[rows][cols];
JTextArea screen = new JTextArea();
JPanel bpanel = new JPanel();
public static void main(String[] args) {
ButtonScreen bs = new ButtonScreen();
bs.setVisible(true);
}// end of main
public ButtonScreen(){
super("Welcome to the ButtonScreen Program!");
setDefaultCloseOperation(EXIT_ON_CLOSE);
bpanel.setLayout(new GridLayout(rows,cols));
for(int i=0;i<button.length;i++){
for(int j=0;j<button.length;j++){
button[i][j]=new JButton(""+i+j);
bpanel.add(button[i][j]);
}
}
add(bpanel);
}//end of constructor
}//end of class
This works just fine, but it creates buttons with "labels" (meaning string parameters at line 26) and also these labels, are not one string or one integer but it is a dillusion of the of i right next to j counter. So my second attempt, after some corrections, was:
import java.awt.*;
import javax.swing.*;
public class LabArr extends JFrame{
JFrame frame = new JFrame();
int rows=10;
int cols= 10;
int i=0;
JButton[][] button = new JButton[rows][cols];
JLabel[] label = new JLabel[100];
public static void main(String[] args) {
LabArr la = new LabArr();
la.setVisible(true);
}//end of main
public LabArr(){
super("title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(rows,cols));
for(i=0;i<label.length;i++){
label[i]= new JLabel(toString(i));
}
for(int i=0;i<button.length;i++){
for(int j=0;j<button.length;j++){
button[i][j]= new JButton(""+label[j]);
add(button[i][j]);
}
}
}//end of constructor
public String toString(int k){
k=i;
String s;
s=""+k;
return s;
}
}//end of class
My goal using that was to create an array of JLabel objects and then match every JLabel element to one from the JButton array.
So, first of all I would like to know to which object the methods setLayout and setDefaultCloseOperation refer to.
Second, "Does the toString method need to refer to an object at the line I am using it?". And finally, what am I missing?
This will create a single frame with buttons from 1 to 100.
import javax.swing.*;
import java.awt.GridLayout;
public class HundredButtonGrid{
public static void main(String[] args){
JFrame frame = new JFrame("100 buttons");
frame.setLayout(new GridLayout(10, 10));
for(int i = 0; i<100; i++){
frame.add( new JButton( "" + (i + 1) ) );
}
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I've addressed a few things.
I just use 1 index because the GridLayout takes care of the x/y values.
I used parenthesis to add the index to 1 so that it doesn't end up concatenating them by string.
I've set the default close operation to exit, you can have it do other things too. Like JFrame.DISPOSE_ON_CLOSE so the window will go away, but not terminate the application.
I didn't extend JFrame, I created a separate instance. In your case you have extended JFrame, so when you call setLayout it is being called on the instance you're creating. An alternative way to say it is this.setLayout same with setDefaultCloseOperation.
Hope this will help you with your problem, kindly see the code below:
public class LabArr extends JFrame{
JFrame frame = new JFrame();
int rows=10;
int cols= 10;
int counter = 0;
JButton[][] button = new JButton[rows][cols];
JLabel[] label = new JLabel[rows*cols];
public static void main(String[] args) {
DemoApplication la = new DemoApplication();
la.setVisible(true);
}//end of main
public LabArr(){
super("title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(rows,cols));
for(int i=0;i<label.length;i++){
label[i]= new JLabel(toString(i));
}
for(int i=0;i<button.length;i++){
for(int j=0;j<button.length;j++){
button[i][j]= new JButton(counter + "");
add(button[i][j]);
counter++;
}
}
}//end of constructor
public String toString(int k){
String s;
s=""+k;
return s;
}
}
You've been creating the buttons with the label toString() method name.
Related
I've been working on this project for an assignment and I've been stuck on this problem. I new and don't understand much of the programming jargon so if someone could help explain why my program isn't working that would be great.
The programs purpose is to display a randomly generated matrix of 1's and 0's in a 10x10 layout and have some buttons on the top that have functions. I'm just stock on how to get everything to display.
Thanks in advance.
UPDATE:: Told providing all my code would help
public class Module5 extends JFrame {
private static JTextArea area = new JTextArea();
private static JFrame frame = new JFrame();
private static JPanel general = new JPanel();
private static JPanel buttons = new JPanel();
private static JPanel numbers = new JPanel();
private static JButton button0 = new JButton("Reset to 0");
private static JButton button1 = new JButton("Resset to 1");
private static JButton buttonReset = new JButton("Reset");
private static JButton quit = new JButton("Quit");
public static class Numbers extends JPanel {
public Numbers() {
area.setText(Integer.toString((int) Math.round(Math.random())));
this.add(area);
}
public void Module5(){
numbers.setLayout(new GridLayout(10, 10));
for (int i = 0; i < 100; i++) {
this.add(new Numbers());
}
}
}
public static void main (String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
general.setLayout(new BoxLayout(general, BoxLayout.Y_AXIS));
general.add(buttons);
general.add(numbers);
buttons.add(button0);
buttons.add(button1);
buttons.add(buttonReset);
buttons.add(quit);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
}
Since this does look like homework I'll give you some pointers but am not going to give you the code.
Move your constructor for Module5 out of the numbers class and into its own class. Also remove the void return type from this to make it a correct constructor.
Move the code in your main into the constructor for Module5. This is the main frame so when you build a new one it should be initialised here, not in main. And remove the setVisible call for now (this is addressed in number 6)
After doing 1 and 2, get rid of your frame variable, your Module5 is a JFrame so anything to do with frame can just be changed to the keyword this (meaning this Module5 object)
Also move the area variable to be within the Numbers class - otherwise every Number is essentially going to share the same text area and This is not what you want.
Don't have your variables as static they should not need to be.
Once this is all done make sure it is running on the Event Dispatch Thread by making your main method like this (the one piece of code I will give you)
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
Module5 mod5 = new Module5();
mod5.setVisible(true);
}
});
}
I am struggling with the below assignment:
assignment q:
using methods from the string class, write a program that will count the number of words which are separated by blanks in a string. For simplicity, use strings without punctuation or other white space characters(tabs, newlines etc). Use a JTextArea to allow the user to enter the text and allow the text area to scroll if necessary. when the user clicks a button to count the words , the total number of words counted is displayed in a textbox that cannot be modified by the user.
now my problem is that i am not getting the counted number to display in the un-editable textbox.
i also have the problem where the cusrsor is showing in the middle of the input screen instead of at the top.
please can you point me in the right direction.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public class WordCounter extends JFrame implements ActionListener
{
//Construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
//Construct labels and text boxes
JTextField screen = new JTextField(1);
JLabel wordCount = new JLabel(" Word Count = ");
JTextField words = new JTextField(3);
//Construct button
JButton countButton = new JButton("Count Words");
public static void main(String[] args)
{
WordCounter f = new WordCounter();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setTitle("Word Counter");
f.setResizable(false);
f.setLocation(200,200);
f.setVisible(true);
}
public static int getWordCount(String screen)
{
int count = 0;
for (int i = 0; i < screen.length(); i++)
{
if (screen.charAt(i) == ' ')
{
count++;
}
}
return count;
}
public WordCounter()
{
Container c = getContentPane();
c.setLayout((new BorderLayout()));
fieldPanel.setLayout(new GridLayout(1,1));
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//add rows to panels
fieldPanel.add(screen);
//add button to panel
buttonPanel.add(countButton);
buttonPanel.add(wordCount);
buttonPanel.add(words);
//add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add functionality to button
countButton.addActionListener(this);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?", "File Submission",JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
}
}
To display the word count, you must modify your actionPerformed method like so:
public void actionPerformed(ActionEvent e)
{
words.setText(String.valueOf(getWordCount(screen.getText())));
}
And also your method for counting words produces incorrect result if the entered text doesn't end with a space. You could modify your getWordCount method for example like this to get correct word count:
public static int getWordCount(String screen)
{
String[] words = screen.split("\\s+");
return words.length;
}
And for your second problem: your cursor displays in the center because JTextField is a single line input. Use JTextArea instead. It is after all specified in your question that you should use it:
JTextArea screen = new JTextArea();
try it:
public static int getWordCount(String screen)
{
int count = 0;
string[] words = screen.split(' ');
count = words.Length;
return count;
}
so I am trying to make a small java program to count the number of characters in a given string.
Right now, my code works in the console output, but it is not updating the text field I have created in a JFrame and I am unsure why. Can someone please explain this to me?
BTW: I have set "onPress" to true, so the button doesn't really have an effect right now, I'm just trying to test the functionality of the program before I implement button functionality.
Thank you :)! Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class characterCounterTwov2 extends JFrame {
static boolean onPress = true;
public characterCounterTwov2(){
/*******************/
/* Local Variables */
/*******************/
//creates a new Jframe to put our frame objets in
JFrame frame = new JFrame();
//creates a text field frame object
JTextField txtField = new JTextField("Enter your text here", 25);
//stores the string of the the jtextfield into a variable text
String text = txtField.getText();
//creates a text field that is uneditable with the word "characters"
String charString = "Characters: ";
JTextField charField = new JTextField(charString, 25);
charField.setEditable(false);
//integer to count the characters
int charCounter = 0;
//string that will be used in a text field to display the # of chars
String charCount = Integer.toString(charCounter);
//Text field that displays charCount
JTextField charFieldTwo = new JTextField(charCount, 10);
//calculate button
JButton calcButton = new JButton("Calculate");
calcButton.addActionListener(new calcButtonFunc());
/*******************/
/* Frame Setup */
/*******************/
//sets the layout of the frame
frame.setLayout(new BorderLayout());
//add's elements to the frame
frame.add(txtField, BorderLayout.NORTH);
frame.add(charField, BorderLayout.CENTER);
frame.add(charFieldTwo, BorderLayout.SOUTH);
frame.add(calcButton, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
//begin while loop
//infinite while loop
System.out.println("Entering main while loop");
while(true)
{
while(onPress == true)
{
System.out.println("text length is:" + charCount);
for(int i = 0; i < text.length(); i++)
{
charCounter++;
System.out.println("Number of characters:" + charCounter);
}
//charCount = Integer.toString(charCounter);
onPress = false;
}
}
}
static class calcButtonFunc implements ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
onPress = true;
}
}
public static void main(String[] args){
new characterCounterTwov2();
System.out.println("End of program. Should not get here");
}
}
~~~~~~~~~
EDIT
I was given a lot of helpful tips by Hovercraft Full of Eels, and cleaned up my code. I am still having a problem with calculating the character count on button press, and having it reflect in the GUI. I am thinking my bug lies within the line "text = txtField.getText();" inside my button listener class.
Here is my updated code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class characterCounterTwov4{
//creates a new Jframe to put our frame objets in
JFrame frame = new JFrame();
//creates a text field frame object
JTextField txtField = new JTextField(25);
//stores the string of the the jtextfield into a variable text
String text = txtField.getText();
//creates a text field that is uneditable with the word "characters"
String charString = "Characters: ";
JTextField charField = new JTextField(charString, 25);
public characterCounterTwov4(){
charField.setEditable(false);
//integer to count the characters
int charCounter = 0;
//string that will be used in a text field to display the # of chars
String charCount = Integer.toString(text.length());
//Text field that displays charCount
JTextField charFieldTwo = new JTextField(charCount, 10);
//calculate button
JButton calcButton = new JButton("Calculate");
calcButton.addActionListener(new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent event)
{
System.out.println("button pressed");
//stores the string of the the jtextfield into a variable text
text = txtField.getText();
//string that will be used in a text field to display the # of chars
String charCount = Integer.toString(text.length());
//Text field that displays charCount
JTextField charFieldTwo = new JTextField(charCount, 10);
}
});
/*******************/
/* Frame Setup */
/*******************/
//sets the layout of the frame
frame.setLayout(new BorderLayout());
//add's elements to the frame
frame.add(txtField, BorderLayout.NORTH);
frame.add(charField, BorderLayout.CENTER);
frame.add(charFieldTwo, BorderLayout.SOUTH);
frame.add(calcButton, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
new characterCounterTwov4();
System.out.println("End of program. Should not get here");
}
}
Your code is not structured for correct Swing GUI event-driven programming but rather looks like a linear console program that is being shoe-horned into a GUI. Get rid of that while loop, get rid of that static boolean variable. Instead give your class a non-static (instance) int counter variable and a JTextField non-static variable and simply increment your counter in the actionPerformed method and display the results in the JTextField (or JLabel if you prefer).
So the actionPerformed could look something like:
public void actionPerformed(ActionEVent e) {
counter++; // increment the counter variable
charCount.setText("Count: " + counter); // display the results
}
and again, counter and charCount are non-static and are declared and initialized in the class, not inside of the constructor or any method.
Edit
Additional notes:
Why does your class extend JFrame when you don't use it as a JFrame?
In general, it's a good idea to start all Swing proggrams on the Swing Event Dispatch Thread or EDT. This can be done by passing a Runnable with your start-up code in it to the SwingUtilities static method invokeLater(...).
For example:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
// create and display your GUI from in here
MainGui mainGui = new MainGui();
JFrame mainFrame = new JFrame("Main GUI");
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.add(mainGui);
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
Edit 2
Oops, I read your requirements wrong. You need to count the chars in a String, and so you will need another class instance JTextField, one to hold the user's String, get rid of the counter instance field since it no longer will be needed, and then in the actionPerformed method, simply get the String from the JTextField, get its length, and display the length in another JTextField or in a JLabel, again your choice.
Edit 3
Your code is now almost there!
Problems:
The charFieldTwo variable is a non-final local variable. To be able to use it in your anonymous inner class, either make it final, and note that doing this won't harm its ability to work since it is a reference variable, not a primite,
Or you could move its declaration out of the constructor, like you do the other variables.
Once you've done this, it is easy to call charFieldTwo.setText(charCount); on it at the bottom of your ActionListener, and you're pretty much done.
I am working on a java program that solves sudoku puzzles. So far all I have coded is a class that draws the board with swing, and another class that calls the gui class.
When I try and run the program nothing happens. No error messages are shown, but the gui doesn't show either. It immediately terminates.
Here is my code so far:
Gui class:
package sudoku;
import java.awt.*;
import javax.swing.*;
public class Gui {
Gui gui;
JPanel board;
JPanel subBoard[][];
GridLayout layout;
JLabel square[][];
public void load() {
gui = new Gui();
gui.setUp();
gui.buildBoard();
}
private void setUp() {
layout = new GridLayout(3, 3);
board = new JPanel(layout);
subBoard = new JPanel[3][3];
square = new JLabel[9][9];
}
private void buildBoard() {
// set up board
board.setSize(800, 600);
board.setVisible(true);
int mod = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// add subBoards to board
subBoard[i][j] = new JPanel();
board.add(subBoard[i][j]);
subBoard[i][j].setLayout(layout);
subBoard[i][j].setVisible(true);
// add textfields to each subBoard
square[i + mod][j + mod] = new JLabel();
subBoard[i][j].add(square[i + mod][j + mod]);
square[i + mod][j + mod].setVisible(true);
}
mod += 3;
}
}
}
main class:
package sudoku;
public class SudokuSolver {
public static void main(String[] args) {
Gui gui = new Gui();
gui.load();
}
}
I tried running it in both eclipse and netbeans but got the same result both times. Why does this not work?
There is no displayable window such as a JFrame being used in the application.
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("New GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gui gui = new Gui();
gui.load();
frame.add(gui.getBoard()); // add getBoard getter
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
Remove the class member variable gui within Gui. This is shadowing variables in the outer class Gui so use the latter instead. Also override getPreferredSize to give the board a size when frame.pack() is invoked.
Make GUI extend JFrame firstly. Then in your main method call gui.setVisible(true);.
public class Gui extends JFrame { }
Then in main.
Gui gui = new Gui();
gui.load();
gui.setVisible(true);
You can not show directly Jpanel. for show gui you must use JFrame or any other Window class(JDialog, JWindow...) and after that set visible property true.
setVisible(true);
public class Gui extends JFrame {
Gui(){
...
setVisible(true);
setSize(300,400);
}
...
}
I'm trying to create a board for a game, i first made a frame then if the user //enters the rows and columns as numbers and pushes the start button, it should remove all //whats on frame and add a panel with a grid layout having buttons everywhere
Here is the code ( Problem is the frame gets cleared and nothing appears)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Frame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton newButton;
JButton Start;
JTextArea row;
JTextArea col;
JLabel background;
JLabel rows;
JLabel columns;
JLabel Error;
JPanel myPanel;
JCheckBox box;
public Frame()
{
//adding frame
setTitle("DVONN Game");
setSize(1000, 700);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
//making start button
Start = new JButton(new ImageIcon("Start"));
Start.setBounds(500, 30, 300, 300);
Start.setOpaque(true);
Start.addActionListener(this);
//make background
background = new JLabel();
background.setBounds(0, -300, 2000, 1500);
background.setIcon(Color.BLUE));
rows = new JLabel("Enter the rows");
columns = new JLabel("Enter the columns");
rows.setForeground(Color.WHITE);
columns.setForeground(Color.WHITE);
rows.setBounds(10,10,100,30);
columns.setBounds(10,45,105,30);
row = new JTextArea();
col = new JTextArea();
row.setBounds(120,10,100,30);
col.setBounds(120,45,100,30);
Error = new JLabel("Enter numbers plz!");
Error.setBounds(10, 100, 400, 30);
Error.setForeground(Color.RED);
Error.setVisible(true);
box = new JCheckBox("Enable Random Filling");
box.setBounds(10, 200, 150, 20);
box.setVisible(true);
myPanel = new JPanel();
myPanel.setBounds(30, 30, 700, 500);
myPanel.setVisible(true);
newButton = new JButton();
newButton.setOpaque(true);
getContentPane().add(box);
getContentPane().add(rows);
getContentPane().add(columns);
getContentPane().add(row);
getContentPane().add(col);
getContentPane().add(Start);
getContentPane().add(background);
this.validate();
this.repaint();
}
public static void main(String[]args)
{
new Frame();
}
//adding actions for start button
public void actionPerformed(ActionEvent e) {
boolean flag = true;
String r1 = row.getText();
String c1 = col.getText();
int x = 0,y = 0;
try{
x = Integer.parseInt(r1);
y = Integer.parseInt(c1);
} catch(NumberFormatException l) {
flag = false;
}
int size = x * y;
if (flag == true) {
this.getContentPane().removeAll();
this.validate();
this.repaint();
myPanel.setLayout(new GridLayout(x, y));
while(size != 0)
{
myPanel.add(newButton);
size --;
}
this.getContentPane().add(myPanel);
} else {
this.getContentPane().add(Error);
}
}
}
There are several issues with this code
Is it really needed to post that much code. A simple UI with one button to press, and then another component which should appear would be sufficient for an SSCCE
The use of null layout's. Please learn to use LayoutManagers
Each Swing component can only be contained once in the hierarchy. So this loop is useless since you add the same component over and over again (not to mention that a negative size would result in an endless loop)
while(size != 0){
myPanel.add(newButton);
size --;
}
Have you tried debugging to see whether size is actually >0. Since you silently ignore ParseExceptions you might end up with a size of 0 which will clean the content pane and add nothing
Then do as goldilocks suggests and call validate after adding the components. See the javadoc of the Container#add method
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.
Call validate() and repaint() after the new elements have been added instead of after the old ones have been removed.
You don't need to be calling setVisible() on individual components, call it after pack() on the Frame itself, and you shouldn't use validate() and repaint() in the constructor. Ie, replace those with:
pack();
setVisible(true);
or you can do that on the object after the constructor is called.
Try to replace
public static void main(String[]args)
{
new Frame();
}
by
public static void main(String[]args)
{
new Frame().setVisible(true);
}
Remove the call to this.setVisible in the constructor and make this your main method.
public static void main(String[] args) {
final Frame fr = new Frame();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
fr.setVisible(true);
}
});
}
This will make sure that the frame elements will be in place before it becomes visible.