java.AWT - setSize() method - java

I am facing an issue using setSize() method in the below program.
Error : The method setSize(int,int) is not defined for the type frame.
When I see Java API, "Class Frame" has this Method inherited from class
java.awt.Window. As i have instantiated the Frame class, this object should have setSize() method as Frame is derived class of Window.
Why am I getting this error then? How can a derived class doesnt contain its superclass method?
public class AwtPrac{
public static void main(String[] args) {
Frame fm = new Frame("Java Programm");
Button b= new Button ("Click Here");
fm.add(b);
fm.setVisible(true);
fm.SetSize(300,300);
fm.dispose();
}
}

Take this code
import java.awt.Frame;
public class AwtPrac {
private static void init(){
Frame fm = new Frame("Java Programm");
fm.setTitle("AwtPrac");
fm.setSize(300,300);
fm.setVisible(true);
}
public static void main(String[] args) {
init();
}
}

Related

Is there a way to call setBackgroud() in an exented class of JPanel

I'm setting up a simulation of Conway's game of life. I wanted to create a set of boxes that are of type 'Mybox' that extends 'JPanel'. Inside of each of these boxes I would like to call the function SetBackground() when the program starts. Here is the closest Ive came to getting it working
package conwaysGameOfLife;
import java.awt.Color;
import java.awt.Panel;
import javax.swing.JPanel;
public class MyBox extends JPanel{
public void setBackground(Color color){
super.setBackground(color);
}
public static void main(String[] args) {
setBackground(Color.white);
}
}
When I enter this I receive errors telling me to make setBackground() static but when I do I get an error under the supper keyword.
setBackground() should not be static.
In your main(), you need to create an instance of MyBox and use that instance:
MyBox box = new MyBox();
box.setBackground( Color.red );
For example:
public class MyBox extends JPanel{
public MyBox() {
this( Color.GREEN );
}
public MyBox(Color color){
setBackground(color);
}
public static void main(String[] args) {
// Create an instance with green background
// using the default constructor:
MyBox greenBox = new MyBox();
// or use the other constructor
MyBox redBox = new MyBox(Color.RED);
// then later you can change the color:
redBox.setBackground(Color.white);
}
}

Passing objects to a gui in java

I'm currently in a java class and I'm trying to work on build a my first interface. The one below is not the one I'm working on, but a test one so I can figure out how to do what I want to do. Here is the code, the question will follow:
package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.*;
/**
*
* #author StrifeX
*/
public class Test {
public static class buttonTest extends JFrame {
private int number;
private JButton push;
public ButtonTest(){
setLayout(new GridBagLayout ());
//Creates an instance of the layout
GridBagConstraints panel = new GridBagConstraints();
// Establishes the pixels suronding each object within the layout
panel.insets = new Insets (5, 5, 5, 5);
//create the withdraw button and its properties
push = new JButton("push");
panel.fill = GridBagConstraints.HORIZONTAL;
panel.gridx = 0;
panel.gridy = 0;
panel.gridwidth = 1;
add(push, panel);
MyEvent buttonClick = new MyEvent();
push.addActionListener(buttonClick);
}//end constructor
public class MyEvent implements ActionListener {
public void actionPerformed (ActionEvent buttonClick){
String operation = buttonClick.getActionCommand();
if(operation.equals("Withdraw")){
JOptionPane.showMessageDialog( null, "test" );
}
}
}
}//end button test
public static class TestObject{
int testField;
public void testObject(){
testField = 10;
}
public void setterMethod(int newInt){
this.testField = newInt;
}
public int getterMethod(){
return this.testField;
}
}
public static void main(String[] args)
{
TestObject obj1 = new TestObject();
buttonTest button = new buttonTest();
// Establish basic parameters for the GUI
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setLocationRelativeTo(null);
button.setVisible(true);
button.setSize(450, 350);
button.setTitle("Change the Balance");
}
}
What I want to do is build an object, then use that objects methods with the GUI button. I can build the object, and pass it to the constructor that make the GUI (buttonTest), but the Event class cant see that object, and from the little I know, I can't pass it to the event class, but maybe the actionPerformed method like this:
public void actionPerformed (ActionEvent buttonClick, testObject thing)
However, when I tried to do that, I received and error that said
buttonTest.event is not abstract and does not override abstract method
actionPerformed(actionEvent) in ActionListener.
Any help would be greatly appreciated. Thank you.
The problem is wrongly defined the following:
public void actionPerformed (ActionEvent buttonClick, testObject thing)
The correct definition will be:
public void actionPerformed (ActionEvent buttonClick)
Getting passed all your previous issues and getting to the heart of the question
Passing objects to a gui in java
You basically need to pass the TestObject to all the classes which need access to it.
Start by modifying your MyEvent class to take a reference to TestObject
public class MyEvent implements ActionListener {
private TestObject obj;
public MyEvent(TestObject obj) {
this.obj = obj;
}
#Override
public void actionPerformed(ActionEvent buttonClick) {
String operation = buttonClick.getActionCommand();
// Now you have acces to obj
if (operation.equals("Withdraw")) {
JOptionPane.showMessageDialog(null, "test");
}
}
}
This will require you to modify your ButtonTest class to allow it to pass the TestObject through to the event handler...
public static class ButtonTest extends JFrame {
//...
public ButtonTest(TestObject obj) {
//...
MyEvent buttonClick = new MyEvent(obj);
//...
And finally, you create and pass the object to the instance of ButtonTest which needs it
public static void main(String[] args) {
TestObject obj1 = new TestObject();
ButtonTest button = new ButtonTest(obj1);
// Establish basic parameters for the GUI
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setLocationRelativeTo(null);
button.setVisible(true);
button.setSize(450, 350);
button.setTitle("Change the Balance");
}

Implement 2 classes for ActionListener for buttons

I have 2 classes, the first class is where I am creating GUI and all of the components needed. Including the buttons. This is being done outside of the main method and in there own respective methods. I want to .addActionListener, but from another class outside of this one. I do not want to use inner classes.
Here is the classes containing Main and the Gui components and the button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class PasswordGeneratorGui {
private JFrame interfaceFrame;
private JPanel interfacePanel;
private JMenuBar interfaceMenuBar;
private JMenu interfaceMenu;
private JMenuItem interfaceMenuItemFile;
private JButton interfaceButtonGenerate;
public static void main(String[] args) {
new PasswordGeneratorGui();
}
public PasswordGeneratorGui() {
createInterfacePanel();
createInterfaceFrame();
createInterfaceMenuBar();
createInterfaceMenu();
createInterfaceMenuItem();
createInterfaceButton();
PasswordGeneratorButtonHandler b = new PasswordGeneratorButtonHandler();
interfaceFrame.add(interfacePanel);
interfaceFrame.setVisible(true);
}
public void createInterfacePanel() {
interfacePanel = new JPanel();
interfacePanel.setLayout(null);
}
public void createInterfaceFrame() {
interfaceFrame = new JFrame();
interfaceFrame.setTitle("Password Generator");
interfaceFrame.setBounds(50, 50, 700, 400);
interfaceFrame.setResizable(false);
interfaceFrame.setJMenuBar(interfaceMenuBar);
}
public void createInterfaceMenuBar() {
interfaceMenuBar = new JMenuBar();
interfaceMenuBar.setBounds(0, 0, 700, 20);
interfaceMenuBar.setVisible(true);
interfacePanel.add(interfaceMenuBar);
}
public void createInterfaceMenu() {
interfaceMenu = new JMenu("File");
interfaceMenuBar.add(interfaceMenu);
}
public void createInterfaceMenuItem() {
interfaceMenuItemFile = new JMenuItem("Exit");
interfaceMenu.add(interfaceMenuItemFile);
}
**public void createInterfaceButton() {
interfaceButtonGenerate = new JButton("Generate");
interfaceButtonGenerate.setBounds(0, 358, 700, 20);
interfaceButtonGenerate.addActionListener();
interfacePanel.add(interfaceButtonGenerate);
}**
}
Here is the class for the ActionListener
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PasswordGeneratorButtonHandler implements ActionListener {
PasswordGeneratorButtonHandler generate = new PasswordGeneratorButtonHandler();
public PasswordGeneratorButtonHandler() {
}
public void interfaceButtonGenerateHandler(ActionEvent event) {
System.exit(1);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
I just want to be able to call the AcitonListener method from the second class. I have tried initiating a new instance of the class and calling it however I think I wasn't quite going in the correct direction.
I'm a little confused about what your asking. You said
I just want to be able to call the AcitonListener method from the second class
Taken literally this means that while you're inside of the PasswordGeneratorButtonHandler class, you want to call the actionPerformed() method. If so, just use this.actionPerformed(), where this is a special keyword in java, representing the current instance of your class.
If however you want to add your handler to the button you created in the first class, which seems like what you might want to do, then you just need to call the JButton#addActionListener() method.
public PasswordGeneratorGui() {
createInterfacePanel();
createInterfaceFrame();
createInterfaceMenuBar();
createInterfaceMenu();
createInterfaceMenuItem();
createInterfaceButton();
PasswordGeneratorButtonHandler b = new PasswordGeneratorButtonHandler();
interfaceButtonGenerate.addActionListener(b); // Add handler to button
interfaceFrame.add(interfacePanel);
interfaceFrame.setVisible(true);
}
Also, inside of the PasswordGeneratorButtonHandler class, you instantiate an instance of the class called generate. This is unnecessary.

java temperature conversion in GUI

im struggling again, with doing a program in java that converts the temperature from celsius to fahrenhiet, but it must be done in a GUI so that the user may enter in a number for celsius and click a botton to convert it. ive been modeling it off of a example in my book however in the book it shows it working with out having a static main(), but my IDE gave me an error saying that it was needed, so ive added a static main() and ive tried calling the tempwindow() to see if that would work but still nothing and even if i comment out the call it doesnt give me a error but nothing happens.
im hopping someone can help show me what im doing wrong and how i should go about this.
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;
public class tempcon extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField tempC;
// private JRadioButton tempF;
// private ButtonGroup radioButtonGroup;
private JButton calcButton;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 100;
public tempwindow()
{
setTitle("Temurture convertion");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("enter tempurture in celsius");
tempC = new JTextField(10);
calcButton = new JButton("convert");
calcButton.addActionListener(new CalcButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(tempC);
panel.add(calcButton);
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
double temp;
input = tempC.getText();
temp = Double.parseDouble(input) * 1.8 + 32;
JOptionPane.showMessageDialog(null, "that is " + temp + "degrees fehrenhiet");
}
}
public static void main(String[] args)
{
tempwindow();
}
}
You are doing a few things wrong:
Your constructor can't have a different name from your class name
You need to instantiate the object to call the constructor, rather instantiating an object does call the constructor but you can't just access it like a method.
You should use the Java naming conventions for class names.
The class:
public class TempCon extends JFrame
{
// Variable declarations
public TempCon() // Constructor should match the Class name
{
}
}
The main class:
public static void main(String[] args)
{
TempCon converter = new TempCon();
}
public static void main(String[] args)
{
tempcon myTempWindowInstance = new tempcon();
myTempWindowInstance.tempwindow();
}
You never initialize a tempcon. Your constructor must have the same name as the class, so I recommend the following changes instead:
Replace public tempwindow() with public tempcon() to correct the constructor.
public tempcon()
{
setTitle("Temurture convertion");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
create an instance of tempcon using new, which calls the constructor:
public static void main(String[] args)
{
tempcon myTempWindowInstance = new tempcon();
}
please write return type your function like.
public void tempwindow()
create object of class and call method.
public static void main(String[] args)
{
tempcon t=new tempcon();
t.tempwindow();
}
and learn java object java object oriented programming.

Open another class in java

I recently started learning some java, and I am facing a problem at this moment.
When an if statement is completed, I want a JFrame from another class to pop up.
So I would like something like this:
if(...)
{
//open the JFrame from the other class
}
My JFrame class looks like this:
import javax.swing.*;
public class Game {
public static Display f = new Display();
public static int w = 600;
public static int h = 400;
public static void main(String args[])
{
f.setSize(w, h);
f.setResizable(true);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Frame Test");
}
}
class Display extends JFrame
{
}
Something like this would work:
if(...)
{
//open the JFrame from the other class
OtherClass otherClass = new OtherClass();
otherClass.setVisible(true);
}
You should also initialise your JFrames in the constructor, otherwise your main method will get messy

Categories

Resources