Reputation: 367
I am new to Java JFrame
.
I want the user to insert IP and password, and then click on the runBtn
.
But after when running this code, the display is very small:
How can I make the text box bigger and the screen tidier, so the user can insert value and click the button?
public class Auditgui extends JFrame{
private JPanel mainPanel;
private JPanel textPanel;
private JPanel consolePanel;
private JPanel btnPanel;
private JButton runBtn;
private JTextField ip;
private JTextField password;
private JLabel ipLabel;
private JLabel passwordLabel;
private JTextPane textArea1;
private JScrollPane scroll;
public Auditgui(){
mainPanel = new JPanel();
setContentPane(mainPanel);
textPanel = new JPanel();
ipLabel = new JLabel();
ipLabel.setText("IP");
passwordLabel = new JLabel();
passwordLabel.setText("Password");
ip = new JTextField();
password = new JTextField();
textPanel.add(ipLabel);
textPanel.add(ip);
textPanel.add(passwordLabel);
textPanel.add(password);
consolePanel = new JPanel();
scroll = new JScrollPane();
textArea1 = new JTextPane();
scroll.setViewportView(textArea1);
consolePanel.add(scroll);
btnPanel = new JPanel();
runBtn = new JButton();
btnPanel.add(runBtn);
mainPanel.add(textPanel);
mainPanel.add(consolePanel);
mainPanel.add(btnPanel);
textPanel.setSize(400, 300);
textPanel.setAlignmentX(1);
}
public static void main(String[] args) {
Auditgui auditGui = new Auditgui();
auditGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
auditGui.setSize(700, 500);
auditGui.setTitle("Recorder");
auditGui.setVisible(true);
}
}
Upvotes: 0
Views: 41
Reputation: 51445
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
When creating a Swing GUI, you work from the inside out. First, you define the swing components. Then, you let the Swing layout managers size the JPanels
. Finally, you pack the JFrame
.
Here's a GUI I came up with based on the code you posted in your question.
All Swing applications must start with a call to the SwingUtilities
invokeLater
method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
A JFrame
has a default BorderLayout
. You should create one or more JPanels
to place on the JFrame
. I created a logon JPanel
and a text JPanel
. You should create each JPanel
in a separate method or class. This makes the code easier to understand and makes it easier to rearrange JPanels
if you decide you want a different JPanel
layout.
I used a GridBagLayout
for the logon JPanel
. A GridBagLayout
is useful for creating forms made up of JLabels
and JTextFields
. You should create the Swing components for a JPanel
in column, row order. This makes it easier for people to understand your code.
I used a BorderLayout
for the text JPanel
. I have no idea what this text area is used for. I placed the text area inside of a JScrollPane
so you can type as much text as you wish.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AuditGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new AuditGUI());
}
private JPasswordField passwordField;
private JTextField ipField;
@Override
public void run() {
JFrame frame = new JFrame("Recorder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createLogonPanel(), BorderLayout.WEST);
frame.add(createTextPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createLogonPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 5, 5, 5);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("IP:");
panel.add(label, gbc);
gbc.gridx++;
ipField = new JTextField(20);
panel.add(ipField, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Password:");
panel.add(label, gbc);
gbc.gridx++;
passwordField = new JPasswordField(20);
panel.add(passwordField, gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy++;
JButton button = new JButton("Login");
panel.add(button, gbc);
return panel;
}
private JPanel createTextPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JTextArea textArea = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
}
Upvotes: 1