import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Temperature extends JFrame implements ActionListener{
private JLabel text1,text2;
private JTextField userInput,output;
private JButton cvrt,cvrt1,clr;
public void BuildGUI(){
text1 = new JLabel("Key in your value");
text2 = new JLabel("Answer");
userInput = new JTextField();
output = new JTextField();
cvrt = new JButton("Convert Celcius to Fahrenheit");
cvrt.addActionListener(this);
cvrt1 = new JButton ("Convert Fahrenheit to Celcius");
cvrt1.addActionListener(this);
clr = new JButton("Clear");
clr.addActionListener(this);
Container cpane = getContentPane();
cpane.setLayout(new GridLayout(4,2));
setVisible(true);
cpane.add(text1);
cpane.add(userInput);
cpane.add(text2);
cpane.add(output);
cpane.add(cvrt);
cpane.add(cvrt1);
cpane.add(clr);
}
public void actionPerformed(ActionEvent e){
double fahrenheit,celcius;
if(e.getSource()== cvrt){
String value = userInput.getText();
celcius = Double.parseDouble(value);
fahrenheit = celcius*1.8+32;
String out = ""+fahrenheit;
output.setText(out);
}else if (e.getSource()== cvrt1){
String value = userInput.getText();
fahrenheit = Double.parseDouble(value);
celcius = (fahrenheit-32)/1.8;
String out = ""+ celcius;
output.setText(out);
}else{
userInput.setText(null);
output.setText(null);
}
}
}
Sorry I'm still new to Java. I Was given an assignment to create a tabbedpane. I'm Just Wondering can you call up the class above into the JTabbed Pane below? Because it will be a mess to throw every codes in a class. Been running through a lot of website but still in vain. Hopefully someone can point it.
public class Windows extends JFrame{
public Windows(){
setTitle("Converter");
setSize(null);
JTabbedPane tabby = new JTabbedPane();
getContentPane().add(tabby);
}
Your class Temperature
has to be a JPanel
and you have to create a suituable constructor, which initializes your GUI. Then you could add the JPanel
to your JTabbedPane
in another class like this:
public class Windows extends JFrame{
public Windows(){
setTitle("Converter");
setSize(null);
JTabbedPane tabby = new JTabbedPane();
getContentPane().add(tabby);
Temperature t = new Temperature();
tabby.addTab(t);
}
Ok first of all:
Why the size of JFrame is null?
How can you see something which isn't visible???
Use the method JFrame.setSize(width,height) [ Link ]
Second thing is :
There is a method called JFrame.setVisible(boolean) [ Link ] , have you ever thought why that method is provided?
To make the JFrame Visible ofcourse.
Another Thing is:
There is a method called setDefaultCloseOperation(int value) [ Link ], which defines what to do when JFrame is closed, use this. Here is the Code:
public class Windows extends JFrame{
public Windows(){
setTitle("Converter");
setSize(100,200);
JTabbedPane tabby = new JTabbedPane();
getContentPane().add(tabby);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
Refer Oracle Java docs for rest of the methods:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
AND Finally :
Even with my revised code you won't see tabbed pane correctly because you have to use
LAYOUT MANAGERS [ Link ] inside the JFrame to align,fix and position the Components.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.