Suppose to pull up 3 different windows with different Labels and Strings in the frames. Below is a picture of the projected output
I have three classes.
P1Panel that extends JPanel
P1Frame that extends JFrame
P1Driver used for main
----------------------------------------------------------P1Panel class below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P1Panel extends JPanel
{
private String s0;
public P1Panel(String s0){
{
add(new P1Panel(s0));
}
}
}
---------------------------------------------This is P1Frame class below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P1Frame extends JFrame
{
private String s1;
public P1Frame (String s1){
this.s1 = s1;
{
add(new P1Panel(s1));
}
P1Frame p1 = new P1Frame(s1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.setSize(300,200);
p1.setVisible(true);
}
}
----------------------------------------This is P1Driver class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P1Driver
{
public static void main(String [] args)
{
P1Frame p1 = new P1Frame("This is window 1");
//JFrame f2 = new JFrame("This is window 2");
//JFrame f3 = new JFrame("This is window 3");
p1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.setSize(300,200);
p1.setVisible(true);
}
}
I believe my P1Panel class is correct in that I called a constructor and added it to itself. The text in my label is passing the P1Panel constructor as a parameter
My P1Frame class I am having difficult with. In the constructor I am wanting to make a P1Panel object and add it to the P1Frame. I think I need to pass a string into the P1Frame constructor as a parameter and then pass string to P1Panel?
I believe my Driver class is correct too as I am just putting main here and setting items size and visibility.
I believe my fix is a small one, but I am stuck and unsure in how to do so. When I run the program as is, it runs infinite with nothing popping up.
You have an infinitely recursive constructor in P1Panel class and that is why your program gets stuck. Just remove the line
add(new P1Panel(s0));
when you execute this line in the constructor you are essentially calling it again by doing
new P1Panel(s0)
so the constructor never returns and it keeps calling itself recursively. Plus why are you not adding any Swing components to your P1Panel? It will be empty without Swing components. If you want to display a String inside the panel, I suggest you do the below in the constructor.
setLayout(new BorderLayout());
JLabel label = new JLabel(s0);
add(label, BorderLayout.CENTER);
Related
I'm trying to make chess in java, but I'm stuck on the panel's size and location. When I try to change any of them, it does nothing and I don't know why. Could someone explain it to me?
package Chess;
import javax.swing.*;
import java.awt.*;
public class Window extends Component{
public void addChessWindow() {
JFrame chessWindow = new JFrame("Chess");
chessWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chessWindow.setResizable(false);
chessWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
chessWindow.setUndecorated(true);
chessWindow.setBackground(Color.decode("#4D6713"));
chessWindow.setVisible(true);
JPanel field = new JPanel();
field.setBackground(Color.BLACK);
field.setBounds(30,15,2,1);
chessWindow.add(field);
}
}
Note: everything starts in the main method.
I think you could do something like this:
chessWindow.add(field, BorderLayout.CENTER);
I have run into the issue of not being able to collect the real text from my JTextField when calling getText(). I have provided a simplified version of the code I was running that includes only the aspects of the program to where it will reproduce the issue. I am attempting to collect the text from the JTextField upon the clicking of the button b.
The correct value returned from getText() should be what ever was input, but instead here it simply returns an empty String. In testing I have found that initializing the TJextField with a String will have it return that String no matter what, even if it is changed before pressing the button. and if one uses setText() inside of init() for example, it will continue to return an empty String.
public class TopClass {
public static void main(String[] args){
BottomClass bottom = new BottomClass();
bottom.init();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BottomClass implements ActionListener{
JFrame frame = new JFrame();
JTextField tf = new JTextField();
JButton b = new JButton("BUTTON");
public void init(){
BottomClass bc = new BottomClass();
b.addActionListener((ActionListener) bc);
frame.add(tf, BorderLayout.NORTH);
frame.add(b, BorderLayout.SOUTH);
frame.setSize(100,100);
frame.show();
}
public void actionPerformed(ActionEvent e) {
System.out.println("TEXT: "+tf.getText());
}
}
Several things aren't clean at all in this code, so I just rewrote most of BottomClass.
You can do it by implementing ActionListener, but BottomClass is more than just an EventListener, so (in the name of realistic responsibility) I just added a new instance of ActionListener to the component that needs it (JButton)
You create an instance bc of BottomClass inside the method init() IN BottomClass. This makes no sense at all and was simply deleted.
You initialize your components at the wrong point. They should either be initialized in the constructor, or inside your nice and fitting init() method. I'm 99% sure that the placement of your initializations is what caused your trouble.
I'm not sure how much of an error this is, but when adding your components you specify BorderLayout constraints despite never defining BorderLayout as the LayoutManager to use. I added the setLayout(new BorderLayout()) call.
It's usually good form to have a constructor, especially if you have a different class calling it. Even if it's empty, a written empty constructor is more easily readable and understandable than an invisible one.
All that said, here's a working version of BottomClass, TopClass doesn't need any adjustments:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BottomClass
{
JFrame frame;
JTextField tf;
JButton b;
public BottomClass()
{
// Empty constructor
}
public void init()
{
frame = new JFrame();
frame.setLayout(new BorderLayout());
tf = new JTextField();
b = new JButton("Button");
b.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("TEXT: " + tf.getText());
}
});
frame.add(tf, BorderLayout.NORTH);
frame.add(b, BorderLayout.SOUTH);
frame.setSize(100, 100);
frame.setVisible(true);
}
}
I have a program which displays two buttons and changes the image of one of the buttons on roll over. I am getting an error on my
press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
part, And it looks like this: The method setDefaultCloseOperation(int) is undefined for the type ButtonClass. Even with the exit on close commented out there are more errors, please help.
Main class (with error):
package Buttons;
import javax.swing.JFrame;
public class Main_buttons{
public static void main(String[] args) {
ButtonClass press = new ButtonClass();
press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
press.setSize(300,200);
press.setVisible(true);
}
}
ButtonClass class:
package Buttons;
import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JButton; //BUTTON!!!
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box
public class ButtonClass extends JButton {
private JButton regular;
private JButton custom;
public ButtonClass() { // Constructor
super("The title"); // Title
setLayout(new FlowLayout()); // Default layout
regular = new JButton("Regular Button");
add(regular);
Icon b = new ImageIcon(getClass().getResource("img.png"));
Icon x = new ImageIcon(getClass().getResource("swag.png"));
custom = new JButton("Custom", b);
custom.setRolloverIcon(x); //When you roll over the button that says custom the image will change from b to x
add(custom);
Handlerclass handler = new Handlerclass();
regular.addActionListener(handler);
custom.addActionListener(handler);
}
private class Handlerclass implements ActionListener { // This class is inside the other class
public void actionPerformed(ActionEvent eventvar) { // This will happen
// when button is
// clicked
JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand()));//Opens a new window with the name of the button
}
}
}
I have searched everywhere for this problem and found nothing. Please tell me how to resolve this issue about exiting my window.
Thanks!
You're a little confused as you're creating a class that extends JButton, and calling setVisible(true) on it as if it were a top-level window such as a JFrame or JDialog, and that doesn't make sense. Since it isn't a top-level window it also makes sense to not have a default close operation or understand what that means.
I suggest that you call this method only on top-level windows such as on a JFrame or JDialog and the like. As a side recommendation, I usually avoid setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); and instead more often do setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); which gives it a little more flexibility.
Edit: actually, just change your class to extends JFrame not extends JButton.
Make sure your image path to your resources is correct. For example:
that method is defined for JFrame, not JButton. You're calling it on an instance of a class that extends JButton
The JFrame.Exit_on_close must be used in a JFrame, and you are extending from JButton.
To set a JButton to close a JFrame its something like this.
public class MyClass extends JFrame implements ActionListener{
private JButton button = new JButton("Button");
private JPanel panel = new JPanel();
public static void main(String args[]) {
new MyClass();
}
public MyClass() {
setSize(300, 300);
button.addActionListener(this);
panel.add(button);
add(panel);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
this.dispose();
}
}
OK, so after rethinking my design of the payroll calculator, I am trying to modularize the program into reusable pieces. I am starting off by creating a JComboBox in one class, adding it to a JFrame that I am creating in another class and then calling the JFrame in my main.
When I tested my combo box alone, it worked. However, when I create it in a class and add it to the window class, I am loosing the String array that I added. Any ideas where I am going wrong?
My Main class:
import javax.swing.*;
import java.awt.*;
public class WindowTesting
{
public static void main(String[] args) {
CreateWindow gui = new CreateWindow();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CreateCombo deptBox = new CreateCombo();
}
}
My Window class
import javax.swing.*;
import java.awt.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author bsmith624
*/
public class CreateWindow extends JFrame {
public CreateWindow() {
JFrame frame1;
CreateCombo box1 = new CreateCombo();
frame1 = new JFrame("Department Combo Box");
frame1.setSize(400,200);
frame1.add(box1);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
}
And finally my JComboBox class:
import javax.swing.*;
import java.awt.*;
public class CreateCombo extends JComboBox {
public static String [] deptList = {"Marketing","IT","Accounting","Development","Payroll","Facilities"};
/**Creates the combo box
* with department names
*/
public CreateCombo () {
JComboBox combo = new JComboBox (deptList);
combo.setVisible(true);
}
}
You are creating another JComboBox inside your CreateCombo , is not necesary cause your CreateCombo is a JComboBox
You have to set the model
public CreateCombo () {
super(); // this call JComboBox superConstructor is implicit if you don't put it
this.setModel(new DefaultComboBoxModel(depList));
this.setVisible(true);
}
Or may be a better design would be making this constructor
public CreateCombo(Object[] array ){
super(array);
}
Im not quite sure about your design ,i think you have to review it, you have a class CreateCombo that is a JComboBox may be you don't want this, may you only want a factory of JComboBox.
I am trying code a JPanel in a class (classA) and trying to instantiate it from a class (classB) (where the main method is).
But when I try to code the classA the content assist does not supports. It does not resolves panel related codes, shows syntax error.
What could the problem be?
import javax.swing.*;
import java.awt.*;
public class gui1 {
JFrame j = new JFrame("MY Menu");
j.setSize(900, 700);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setResizable(false);
}
You cannot call methods outside of a method / static initialization block. Try initializing it in the constructor:
public class Gui1 {
JFrame my_frame;
public Gui1()
{
my_frame = new JFrame("MY Menu");
my_frame.setSize(900, 700);
my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my_frame.setResizable(false);
}
}