Invisible GUI? (Java)(Swing)
•
Java
I'm studying this program, using swing Every time I export the program and run it, I try to set up the GUI JFrame can, but it is not an internal component Thank you in advance ~ Ellis
Code:
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Login {
public static void login_Interface(){
//Start GUI style//
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (illegalaccessexception e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
// End //
JFrame login_Frame = new JFrame("Login - LetsMeet");
login_Frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
login_Frame.setSize(750,650);
login_Frame.setResizable(true);
JPanel panel_Title = new JPanel(); //PANEL Title
panel_Title.setBounds(0,750,150);
panel_Title.setLayout(null);
Image logo = null;
try {
logo = ImageIO.read(new File("Data/images/logo_letsmeet.png"));
} catch (IOException e) {
e.printStackTrace();
}
Graphics2D logo_out = ((BufferedImage) logo).createGraphics();
panel_Title.paint(logo_out);
JPanel login_Panel = new JPanel(); //LOGIN Panel
login_Panel.setBounds(0,150,350,150);
login_Panel.setLayout(null);
JTextField username_login = new JTextField("Username");
username_login.setBounds(100,50,100,25);
JPasswordField password_login = new JPasswordField();
password_login.setBounds(200,25);
JButton login_go = new JButton("Login");
login_go.setBounds(200,25);
login_Panel.add(password_login);
login_Panel.add(username_login);
JPanel panel_Divider = new JPanel(); //PANEL Divider
login_Panel.setBounds(350,150);
panel_Divider.setSize(50,100);
panel_Divider.setLayout(null);
Image sep = null;
try {
sep = ImageIO.read(new File("Data/images/sep.png"));
} catch (IOException e) {
e.printStackTrace();
}
Graphics2D div = ((BufferedImage) sep).createGraphics();
panel_Title.paint(div);
JPanel register_Panel = new JPanel(); //REGISTER Panel
register_Panel.setBounds(400,150);
register_Panel.setLayout(null);
login_Frame.add(panel_Title);
login_Frame.add(login_Panel);
login_Frame.add(panel_Divider);
login_Frame.add(register_Panel);
login_Frame.setVisible(true);
}
}
Error: None
Solution
In addition to all the suggestions made by @ madprogammer, you also need to add controls to the JFrame content pane, as shown below:
login_Frame.getContentPane().add(panel_Title); login_Frame.getContentPane().add(login_Panel); ...
Then your control should appear
to update:
Running your actual code and adding colored borders to the containers (jpanels), I get the following:
panel_Title.setBorder(BorderFactory.createLineBorder(Color.BLUE)); login_Panel.setBorder(BorderFactory.createLineBorder(Color.RED)); panel_Divider.setBorder(BorderFactory.createLineBorder(Color.GREEN)); register_Panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
Basically, your code has layout configuration problems Again, follow @ madprogammer's advice You can use this border technique to debug the layout in the future
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
