admin管理员组文章数量:1313387
I am trying to make a frame with a label, (I have created a separate class for the frame), but idk why the label isn´t showing in the frame...
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Frame extends JFrame{
Frame(){
//set title of the frame
this.setTitle("Frame Test nº 1");
//exit out of application
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//define o tamanho da janela
this.setSize(420, 420);
this.setResizable(true);
//create an icon
ImageIcon logo = new ImageIcon("\\\\wsl.localhost\\Ubuntu\\home\\tech\\Java_Codes\\GUI_test\\d6ede8c8-a316-488b-8d87-60dcb1cdbe06.png");
//change icon frame
this.setIconImage(logo.getImage());//get the image created above
//change the colour fron the background
this.getContentPane().setBackground(new Color(0x123456));
this.setVisible(true);
}
}
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class gui{
public static void main (String[] args){
Frame frame = new Frame();
JLabel label = new JLabel();
label.setText("h");
frame.add(label);
}
}
I have tried to change the label inside the Frame class, but it didn't help... I also tried to put frame.setVisible(true);
in the last line of the main...
I am trying to make a frame with a label, (I have created a separate class for the frame), but idk why the label isn´t showing in the frame...
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Frame extends JFrame{
Frame(){
//set title of the frame
this.setTitle("Frame Test nº 1");
//exit out of application
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//define o tamanho da janela
this.setSize(420, 420);
this.setResizable(true);
//create an icon
ImageIcon logo = new ImageIcon("\\\\wsl.localhost\\Ubuntu\\home\\tech\\Java_Codes\\GUI_test\\d6ede8c8-a316-488b-8d87-60dcb1cdbe06.png");
//change icon frame
this.setIconImage(logo.getImage());//get the image created above
//change the colour fron the background
this.getContentPane().setBackground(new Color(0x123456));
this.setVisible(true);
}
}
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class gui{
public static void main (String[] args){
Frame frame = new Frame();
JLabel label = new JLabel();
label.setText("h");
frame.add(label);
}
}
I have tried to change the label inside the Frame class, but it didn't help... I also tried to put frame.setVisible(true);
in the last line of the main...
1 Answer
Reset to default 4Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
I reworked your code and came up with the following GUI.
Seeing one letter is tough, so I increased the font size.
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.
Swing uses layout managers to position Swing components.
Don't extend Swing components unless you override one or more of the class methods.
Don't name your classes the same as Java standard classes, like java.awt.Frame
. It's confusing for readers of your code and yourself after a few months.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JLabelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JLabelExample());
}
@Override
public void run() {
JFrame frame = new JFrame("Frame Test nº 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(50, 200, 50, 200));
panel.setBackground(new Color(0x123456));
JLabel label = new JLabel("h");
label.setForeground(Color.WHITE);
label.setFont(panel.getFont().deriveFont(36f));
panel.add(label);
return panel;
}
}
本文标签:
版权声明:本文标题:java - I have created a separated class for the JFrame, but now the JLabel is not showing up - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741931476a2405598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Color.yellow
– g00se Commented Jan 31 at 0:39add()
: "This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component." → addframe.validate()
after adding the label or consider point 2 of camickr's comment; and previous comment (dark gray on dark blue is hard to see) -- also not recommended to extend a JFrame (or any class) just to call its methods – user85421 Commented Jan 31 at 17:41