What Is Jframe In Java?


how can u put a text box in a Jframe in Java?

how can u put a text box in a Jframe in Java so that you can enter characters there in the new window instead of the compiler?

You mean like this?

JFrame f = new JFrame("Text");
JTextField field = new JTextField();
f.add(field, BorderLayout.SOUTH);
JTextArea area = new JTextArea(30, 80);
f.add(area, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);

That's a simple example just to show how easy it is to add text components to a container, like JFrame. For a more comprehensive tutorial on JFrames and building simple desktop applications, see http://java.sun.com/docs/books/tutorial/uiswing/components/index.html

How do you move from one JFrame to another using JButtons in Java?

I have code written so that there are two separate JFrames, each with JButtons on them. I am trying to figure out how to call the other program when you press the JButton on one JFrame. Any help would be appreciated. Thanks!

create an object of the new jframe
JFrame frame2 = new MyFrame();
frame2.setVisible(true);

check this example
http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html#FrameDemo2
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/FrameDemo2Project/src/components/FrameDemo2.java

How can I add a Fading Effect in JFrame or JWindow?

How can i add a Fading Effect in JFrame(Java Swing) or JWindow, every time they appear and the disappear. Thank You Very Much!

You can only do that with the JPanel, Canvas -- things you draw on. The "chrome" on the JWindow, JFrame is called by the OS. To do a gradiant or fade-in takes Graphics2D.

To fade-in would take a animation loop. It might be possible to make your own Look&Feel for JFrame -- but you have to define the properties for all of the widgets in the Swing GUI library.

How do you make JFrame in java close with a jbutton?

Im making a java project and in addition to having the default close operation as the x button, i want to add a jbutton that also closes the program. How do I make the button do that?

if we pretend the the JFrame object is called "frame" then inside the JButton's click event put

frame.dispose();

How do I place an object in the center of a JFrame?

I've created a JFrame for a programming assignment using Java. The JFrame is 360x360 pixels. I need to place an oval in the center of it. The oval is 90x90 pixels. I know how to add it and everything, but I can't seem to figure out how to place it directly in the center. Thanks for your help!

Because the JFrame is known to be 360 x 360 pixels does not mean the 'drawable area' is that size. You have to take into account the possible title bar, status bar and also the borders around the edge. For this reason you should always use a JPanel that has been added to the JFrame.

There are many ways to organise a 'template' for drawing.. but I find it easiest to have a class that Extends JPanel and then create a JFrame as it's member object. It then 'adds itself' to it's own JFrame. Here is a possible functional template:


import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Draw extends JPanel {

JFrame imageFrame;

public static void main(String[] args) {
Draw d = new Draw();
d.init();
}

public void init() {

imageFrame = new JFrame();
imageFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);

imageFrame.setSize(400, 400);
imageFrame.getContentPane().add(
this);
imageFrame.setVisible(true);
System.out.println("Panel width: " + this.getWidth() + ", Panel height: " + this.getHeight());
this.repaint();
}

@Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
/* code for drawing goes here */

}
}


You can now directly access the size of the JPanel 'Draw' for centering the Oval..

e.g.

int panelWidth = this.getWidth();
int panelHeight = this.getHeight();
int ovalWidth = 90;
int ovalHeight = 90;
g2d.drawOval((panelWidth - ovalWidth) / 2, (panelHeight - ovalHeight) / 2, ovalWidth, ovalHeight);

More Questions