I'm trying to make a Frame that highlights some JLabels and a JtextField, where you can type a host name and in return get the IP address. But my UI is not working as I want it to. I want the size of the UI to be around 200,200 but when I run the program I don't have anything until I maximize and then I can see my label. Here is the code, hopefully you guys can tell me what I have done wrong.
public class Exercise1 extends JPanel {
private JLabel jlblIP = new JLabel("IP= 221.1231.231");
private JLabel jlblName = new JLabel("Namn = DAMSKDLASM");
private JLabel jlblHostIP = new JLabel("Host ip --------");
private JLabel jlblHostName = new JLabel("Host name:");
private JTextField jtxtfield = new JTextField("");
private InetAddress ipaddress;
public Exercise1(){
setPreferredSize(new Dimension(200,200));
add(jlblIP);
}
public static void main(String[] args) {
Exercise1 mainjpn = new Exercise1();
JFrame jframe = new JFrame();
jframe.getContentPane();
jframe.pack();
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.add(mainjpn);
}
}
Just add your Exercise1 JPanel to the JFrame's content pane instead.
Also, don't forget to ensure that any code that touches your UI is done from the event dispatch thread.
Something like:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.net.InetAddress;
public class Exercise1 extends JPanel {
private JLabel jlblIP = new JLabel("IP= 221.1231.231");
private JLabel jlblName = new JLabel("Namn = DAMSKDLASM");
private JLabel jlblHostIP = new JLabel("Host ip --------");
private JLabel jlblHostName = new JLabel("Host name:");
private JTextField jtxtfield = new JTextField("");
private InetAddress ipaddress;
public Exercise1(){
setPreferredSize(new Dimension(200,200));
add(jlblIP);
}
public static void main(String[] args) {
Exercise1 mainjpn = new Exercise1();
Runnable r = new Runnable() {
public void run() {
JFrame jframe = new JFrame();
jframe.getContentPane().add(mainjpn);
jframe.pack();
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
jframe.add(mainjpn) should be placed before pack() and setVisible()
Related
i can't getting updated text of JTextField from other class.
change text of JTextField ( from Names ) and go to Main tabbedPane and click button. updated text not appears on JOptionPane.
this is Frame.java
import java.awt.event.ActionEvent;
public class Frame {
JFrame frame;
JPanel pnl;
JButton btn;
JTabbedPane tabbedPane;
Names n;
public static void main(String[] args) {
Frame x = new Frame();
}
public Frame() {
SwingUtilities.invokeLater(()->Window());
}
public void Window() {
n = new Names();
frame = new JFrame();
tabbedPane = new JTabbedPane();
pnl = new JPanel();
btn = new JButton("get Name");
btn.addActionListener(e->JOptionPane.showMessageDialog(null, n.getName()));
pnl.add(btn);
tabbedPane.addTab("main", pnl);
tabbedPane.addTab("name", new Names());
frame.add(tabbedPane);
frame.setBounds(360, 130, 900, 550);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
this one is Names.java
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Names extends JPanel {
JTextField tf;
public Names() {
tf = new JTextField("test");
tf.setPreferredSize(new Dimension(150,30));
this.add(tf);
}
public String getName() {
return tf.getText();
}
}
thanks for efforts.
Just take a second and have a look at the following...
n = new Names();
//...
btn.addActionListener(e->JOptionPane.showMessageDialog(null, n.getName()));
//...
tabbedPane.addTab("name", new Names());
What do you think is going to happen here?
The instance of Names which the button is interacting with is NOT the instance of Names that the user is interacting with
Change it to...
n = new Names();
//...
btn.addActionListener(e->JOptionPane.showMessageDialog(null, n.getName()));
//...
tabbedPane.addTab("name", n);
On a side note, this...
tf.setPreferredSize(new Dimension(150,30));
is a really bad idea and will come back to haunt you. Instead make use of the setColumns method (or constructor) to set the number of displayed characters the text field should attempt to display
I have been experimenting with making GUIs in java as opposed to just using "static" all the time and come across the "SwingUtilities.invokeLater()" method. I manage to get everything setup but when it comes to run the application, nothing appears on the JPanel until I resize the window. Is there a fix for this or am I doing it wrong?
Heres my code:
public class main extends JPanel implements ActionListener{
public JLabel userLabel;
public JLabel passLabel;
public JTextField userField;
public JTextField passField;
public JButton login;
public JButton closeLogin;
public JButton help;
public main(){
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
add(userLabel);
add(userField);
add(passLabel);
add(passField);
add(login);
add(help);
add(closeLogin);
}
public void actionPerformed(ActionEvent e){
}
public static void initComponents(){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents();
}
});
}
}
EDIT: I know the password JTextField is meant to be a JPasswordField.. so ignore it :P
Two basic advices:
1.) When you use swing, and stuff doesnt show up/update, you should call JPanel.revalidate() and JPanel.repaint() These two functions will update your panel.
If you are using a JFrame and you didn't add any extra panels to it, then you can get the content panel by JFrame.getContentPane()
2.) When you finished adding Components to a panel/frame you should also call pack() on the frame, this will ensure, that all your Components have the prefered size.
You never add your content to the JFrame. The minimal set of changes you need:
public static void main(String args[]){
final main main = new main();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents(main);
}
});
}
And then modify initComponents to take a main object:
public static void initComponents(main main){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
loginFrame.add(main); // <----- this line is added
}
for built_in FlowLayout (for JPanel) I don't suggest to use pack() for JFrame, sure correct way could be to use proper and better LayoutManager for this job, GridBagLayout or SpringLayout
output by using JFrame#setSize() and without pack()
for example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MainLogin implements ActionListener {
private JFrame loginFrame = new JFrame("Encrypted Chat - Login");
private JPanel pnl = new JPanel();
private JLabel userLabel;
private JLabel passLabel;
private JTextField userField;
private JTextField passField;
private JButton login;
private JButton closeLogin;
private JButton help;
public MainLogin() {
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
pnl.add(userLabel);
pnl.add(userField);
pnl.add(passLabel);
pnl.add(passField);
pnl.add(login);
pnl.add(help);
pnl.add(closeLogin);
loginFrame.add(pnl);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainLogin mainLogin = new MainLogin();
}
});
}
}
Put
JFrame.setVisible(true); on the last line after adding all components to it.
I have to get a String input in one JFrame and display in another.
My second task is to flash the given string in a larger font in the second frame, at an interval of 1sec.
How to proceed?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Input{
String hinput;
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private void prepareGUI(){
mainFrame = new JFrame("STRING");
mainFrame.setSize(500,100);
headerLabel = new JLabel("", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
private void showTextField(){
JLabel stringlabel= new JLabel("String ", JLabel.RIGHT);
final JTextField userText = new JTextField(20);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new mylistener());
submitButton.setActionCommand("open");
controlPanel.add(stringlabel);
controlPanel.add(userText);
controlPanel.add(submitButton);
mainFrame.setVisible(true);
}
private class mylistener implements ActionListener{
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("open")){
mainFrame.dispose();
NewJFrame nj= new NewJFrame(hinput);
}
}
}
public static void main(String args[]){
Input Inp = new Input();
Inp.prepareGUI();
Inp.showTextField();
}
}
class NewJFrame{
JFrame mainFrame;
String text;
JLabel l1;
JTextField tb1;
public NewJFrame(String t){
text=t;
mainFrame=new JFrame("STRING");
mainFrame.setSize(800,800);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("Entered string");
tb1.setText(text);
mainFrame.add(l1);
mainFrame.add(tb1);
mainFrame.setVisible(true);
}
}
I am getting traceback after i click 'submit' button.
Please point out the errors.
You can get rid of the error by instatiating tb1 in your NewJFrame class like so:
class NewJFrame{
JFrame mainFrame;
String text;
JLabel l1;
JTextField tb1;
public NewJFrame(String t){
text=t;
mainFrame=new JFrame("STRING");
mainFrame.setSize(800,800);
// *** must init tb1!!! ***///
JTextField tb1 = new JTextField();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("Entered string");
tb1.setText(text);
mainFrame.add(l1);
mainFrame.add(tb1);
mainFrame.setVisible(true);
}
}
As for getting text typed in one JFrame to open in another, I have a slightly modified solution. Maybe have text entered in a JTextField on one JPanel display in another JPanel. To do that, you could use the following code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
public class SimpleGUI extends JFrame {
private final JPanel firstPanel;
private final JPanel secondPanel;
private final JButton submitButton;
private final JTextField textField;
private final JLabel secondPanelLabel;
public SimpleGUI() {
// sets the title of the JFrame
super("SimpleGUI");
setLayout(new FlowLayout());
// inits both JPanels
firstPanel = new JPanel();
secondPanel = new JPanel();
// inits empty second JLabel and adds to the secondPanel
secondPanelLabel = new JLabel();
secondPanel.add(secondPanelLabel);
// makes the secondPanel invisible for the time being
secondPanel.setVisible(false);
// inits the submit button
submitButton = new JButton("Submit");
// event-handler for submit button, will set the text in the
// secondPanelLabel to the text in the JTextField the user types
// into. It then makes the firstPanel (with the text field and button),
// invisible, and then makes the second panel visible.
submitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
secondPanelLabel.setText(textField.getText());
firstPanel.setVisible(false);
secondPanel.setVisible(true);
}
});
// inits the textField
textField = new JTextField(10);
// adds the button and the text field to the firstPanel
firstPanel.add(submitButton);
firstPanel.add(textField);
// adds both panels to this JFrame
this.add(firstPanel);
this.add(secondPanel);
}
}
And here is a class with a main method that constructs the SimpleGUI so you can test it out for yourself:
import javax.swing.JFrame;
public class SimpleGUITest {
public static void main(String[] args) {
SimpleGUI frame = new SimpleGUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
I have been experimenting with making GUIs in java as opposed to just using "static" all the time and come across the "SwingUtilities.invokeLater()" method. I manage to get everything setup but when it comes to run the application, nothing appears on the JPanel until I resize the window. Is there a fix for this or am I doing it wrong?
Heres my code:
public class main extends JPanel implements ActionListener{
public JLabel userLabel;
public JLabel passLabel;
public JTextField userField;
public JTextField passField;
public JButton login;
public JButton closeLogin;
public JButton help;
public main(){
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
add(userLabel);
add(userField);
add(passLabel);
add(passField);
add(login);
add(help);
add(closeLogin);
}
public void actionPerformed(ActionEvent e){
}
public static void initComponents(){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents();
}
});
}
}
EDIT: I know the password JTextField is meant to be a JPasswordField.. so ignore it :P
Two basic advices:
1.) When you use swing, and stuff doesnt show up/update, you should call JPanel.revalidate() and JPanel.repaint() These two functions will update your panel.
If you are using a JFrame and you didn't add any extra panels to it, then you can get the content panel by JFrame.getContentPane()
2.) When you finished adding Components to a panel/frame you should also call pack() on the frame, this will ensure, that all your Components have the prefered size.
You never add your content to the JFrame. The minimal set of changes you need:
public static void main(String args[]){
final main main = new main();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents(main);
}
});
}
And then modify initComponents to take a main object:
public static void initComponents(main main){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
loginFrame.add(main); // <----- this line is added
}
for built_in FlowLayout (for JPanel) I don't suggest to use pack() for JFrame, sure correct way could be to use proper and better LayoutManager for this job, GridBagLayout or SpringLayout
output by using JFrame#setSize() and without pack()
for example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MainLogin implements ActionListener {
private JFrame loginFrame = new JFrame("Encrypted Chat - Login");
private JPanel pnl = new JPanel();
private JLabel userLabel;
private JLabel passLabel;
private JTextField userField;
private JTextField passField;
private JButton login;
private JButton closeLogin;
private JButton help;
public MainLogin() {
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
pnl.add(userLabel);
pnl.add(userField);
pnl.add(passLabel);
pnl.add(passField);
pnl.add(login);
pnl.add(help);
pnl.add(closeLogin);
loginFrame.add(pnl);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainLogin mainLogin = new MainLogin();
}
});
}
}
Put
JFrame.setVisible(true); on the last line after adding all components to it.
I just created an applet
public class HomeApplet extends JApplet {
private static final long serialVersionUID = -7650916407386219367L;
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
// setSize(400, 400);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
RconSection rconSection = new RconSection();
rconSection.setOpaque(true);
// CommandArea commandArea = new CommandArea();
// commandArea.setOpaque(true);
JTabbedPane tabbedPane = new JTabbedPane();
// tabbedPane.setSize(400, 400);
tabbedPane.addTab("Rcon Details", rconSection);
// tabbedPane.addTab("Commad Area", commandArea);
setContentPane(tabbedPane);
}
}
where the fisrt tab is:
package com.rcon;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.Bean.RconBean;
import com.util.Utility;
public class RconSection extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -9021500288377975786L;
private static String TEST_COMMAND = "test";
private static String CLEAR_COMMAND = "clear";
private static JTextField ipText = new JTextField();
private static JTextField portText = new JTextField();
private static JTextField rPassText = new JTextField();
// private DynamicTree treePanel;
public RconSection() {
// super(new BorderLayout());
JLabel ip = new JLabel("IP");
JLabel port = new JLabel("Port");
JLabel rPass = new JLabel("Rcon Password");
JButton testButton = new JButton("Test");
testButton.setActionCommand(TEST_COMMAND);
testButton.addActionListener(this);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand(CLEAR_COMMAND);
clearButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3,2));
panel.add(ip);
panel.add(ipText);
panel.add(port);
panel.add(portText);
panel.add(rPass);
panel.add(rPassText);
JPanel panel1 = new JPanel(new GridLayout(1,3));
panel1.add(testButton);
panel1.add(clearButton);
add(panel);
add(panel1);
// add(panel, BorderLayout.NORTH);
// add(panel1, BorderLayout.SOUTH);
}
#Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals(TEST_COMMAND)){
String ip = ipText.getText().trim();
if(!Utility.checkIp(ip)){
ipText.requestFocusInWindow();
ipText.selectAll();
JOptionPane.showMessageDialog(this,"Invalid Ip!!!");
return;
}
String port = portText.getText().trim();
if(port.equals("") || !Utility.isIntNumber(port)){
portText.requestFocusInWindow();
portText.selectAll();
JOptionPane.showMessageDialog(this,"Invalid Port!!!");
return;
}
String pass = rPassText.getText().trim();
if(pass.equals("")){
rPassText.requestFocusInWindow();
rPassText.selectAll();
JOptionPane.showMessageDialog(this,"Enter Rcon Password!!!");
return;
}
RconBean rBean = RconBean.getBean();
rBean.setIp(ip);
rBean.setPassword(pass);
rBean.setPort(Integer.parseInt(port));
if(!Utility.testConnection()){
rPassText.requestFocusInWindow();
rPassText.selectAll();
JOptionPane.showMessageDialog(this,"Invalid Rcon!!!");
return;
}else{
JOptionPane.showMessageDialog(this,"Correct Rcon!!!");
return;
}
}
else if(arg0.getActionCommand().equals(CLEAR_COMMAND)){
ipText.setText("");
portText.setText("");
rPassText.setText("");
}
}
}
it appears as
is has cropped some data how to display it full and make the applet non resizable as well. i tried setSize(400, 400); but it didnt helped the inner area remains the same and outer boundaries increases
Here's another variation on your layout. Using #Andrew's tag-in-source method, it's easy to test from the command line:
$ /usr/bin/appletviewer HomeApplet.java
// <applet code='HomeApplet' width='400' height='200'></applet>
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class HomeApplet extends JApplet {
#Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
#Override
public void run() {
createGUI();
}
});
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
private void createGUI() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Rcon1", new RconSection());
tabbedPane.addTab("Rcon2", new RconSection());
this.add(tabbedPane);
}
private static class RconSection extends JPanel implements ActionListener {
private static final String TEST_COMMAND = "test";
private static final String CLEAR_COMMAND = "clear";
private JTextField ipText = new JTextField();
private JTextField portText = new JTextField();
private JTextField rPassText = new JTextField();
public RconSection() {
super(new BorderLayout());
JLabel ip = new JLabel("IP");
JLabel port = new JLabel("Port");
JLabel rPass = new JLabel("Rcon Password");
JButton testButton = new JButton("Test");
testButton.setActionCommand(TEST_COMMAND);
testButton.addActionListener(this);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand(CLEAR_COMMAND);
clearButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(ip);
panel.add(ipText);
panel.add(port);
panel.add(portText);
panel.add(rPass);
panel.add(rPassText);
JPanel buttons = new JPanel(); // default FlowLayout
buttons.add(testButton);
buttons.add(clearButton);
add(panel, BorderLayout.NORTH);
add(buttons, BorderLayout.SOUTH);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}
}
As I mentioned in a comment, this question is really about how to layout components in a container. This example presumes you wish to add the extra space to the text fields and labels. The size of the applet is set in the HTML.
200x130 200x150
/*
<applet
code='FixedSizeLayout'
width='200'
height='150'>
</applet>
*/
import java.awt.*;
import javax.swing.*;
public class FixedSizeLayout extends JApplet {
public void init() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initGui();
}
});
}
private void initGui() {
JTabbedPane tb = new JTabbedPane();
tb.addTab("Rcon Details", new RconSection());
setContentPane(tb);
validate();
}
}
class RconSection extends JPanel {
private static String TEST_COMMAND = "test";
private static String CLEAR_COMMAND = "clear";
private static JTextField ipText = new JTextField();
private static JTextField portText = new JTextField();
private static JTextField rPassText = new JTextField();
public RconSection() {
super(new BorderLayout(3,3));
JLabel ip = new JLabel("IP");
JLabel port = new JLabel("Port");
JLabel rPass = new JLabel("Rcon Password");
JButton testButton = new JButton("Test");
testButton.setActionCommand(TEST_COMMAND);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand(CLEAR_COMMAND);
JPanel panel = new JPanel(new GridLayout(3,2));
panel.add(ip);
panel.add(ipText);
panel.add(port);
panel.add(portText);
panel.add(rPass);
panel.add(rPassText);
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
panel1.add(testButton);
panel1.add(clearButton);
add(panel, BorderLayout.CENTER);
add(panel1, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Container c = new RconSection();
JOptionPane.showMessageDialog(null, c);
}
});
}
}
Size of applet viewer does not depend on your code.
JApplet is not window, so in java code you can't write japplet dimensions. You have to change run settings. I don't know where exactly are in other ide's, but in Eclipse you can change dimensions in Project Properties -> Run/Debug settings -> click on your launch configurations file (for me there were only 1 - main class) -> edit -> Parameters. There you can choose width and height for your applet. save changes and you are good to go