-
How to do this
Hi,
I am learning java. so I keep doing things in a different way to learn what I am trying to do
For example here I am trying to call
Code:
package components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* FrameDemo.java requires no other files. */
public class ABC extends JFrame{
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public JLabel label;
/**
* This class should add instruction to the existing frame
* By using a Jlable = label
* A text field
*/
class Text {
public JTextField info, number;
public Text(){
// Text information to the user
info = new JTextField("Enter the number");
info.setEditable(false);
// Empty text field for number entry
number = new JTextField(15);
}
}
public ABC() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
// tried this but dont work
Text info = new Text();
info.add();
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(600, 400));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
// Create and set up the content pane.
//This dont work too
frame.add( new Text());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Can you please show me how should I call the Text() in Text class in the ABC () to make it visible in the frame?
Thank you
-
-
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ABCRx extends JFrame {
// Make this as a member variable so you can access
// the Text fields and methods in this (ABCRx) class.
Text info = new Text();
public JLabel label;
/**
* This class should add instruction to the existing frame
* By using a Jlable = label
* A text field
*/
class Text {
JLabel info; // These are easy to use for information.
public JTextField number;
public Text(){
// Text instruction to the user
info = new JLabel("Enter the number");
// info.setEditable(false);
// Empty text field for number entry
number = new JTextField(15);
}
public JPanel getComponents() {
JPanel panel = new JPanel();
panel.add(info);
panel.add(number);
return panel;
}
}
public ABCRx() {
// Initialize components.
label = new JLabel("Hello world", JLabel.CENTER);
JLabel emptyLabel = new JLabel("almost empty");
emptyLabel.setPreferredSize(new Dimension(600, 400));
// for visibility while you are learning.
emptyLabel.setBorder(BorderFactory.createTitledBorder("empty label"));
// Assemble components.
Container c = getContentPane();
// Poor choice for a top-level container.
// Use the default BorderLayout for now.
// c.setLayout(new FlowLayout());
c.add(label, "First");
c.add(emptyLabel, BorderLayout.CENTER);
c.add(info.getComponents(), "Last");
// JFrame methods. Since this class (ABCRx) extends JFrame
// the class is a JFrame and you can call JFrame methods
// directly inside the class. There is no need to instantiate
// a new JFrame.
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null); // center frame
setVisible(true);
}
public static void main(String[] args) {
new ABCRx();
}
}