我想在swing中一个BorderLayout布局中的north板块加入JPanel面板,显示文字,但无法显示,代码如下
main.javapublicclassMain{publicstaticvoidmain(String[]args){MainFramef=newMainFrame()...
main.java
public class Main {
public static void main(String[] args) {
MainFrame f = new MainFrame();
f.setVisible(true);
}
}
MainFrame.java
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
static int WIDTH = 800;
static int HEIGHT = 600;
CentralController centralController;
UserBar user;
DownloaderMenuBar menuBar;//菜单项
public MainFrame() {
this.setTitle("下载器");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int x = screenSize.width / 2 - WIDTH / 2;
int y = screenSize.height / 2 - HEIGHT / 2;
this.setBounds(x, y, WIDTH, HEIGHT);
init();
}
private void init() {
this.setLayout(new BorderLayout());
user = new UserBar();
this.add(user,"North");
}
}
userBar.java
public class UserBar extends JLabel{
private static final long serialVersionUID = 1L;
CentralController centralController;
JPanel panel;
public UserBar(){
init();
}
private void init(){
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,200,150);
panel.setBackground(Color.CYAN);
JLabel label1 = new JLabel();
label1.setBounds(10, 10, 30, 30);
panel.add(label1);
this.add(panel);
label1.setVisible(true);
this.setBorder(BorderFactory.createEtchedBorder(0));
}
} 展开
public class Main {
public static void main(String[] args) {
MainFrame f = new MainFrame();
f.setVisible(true);
}
}
MainFrame.java
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
static int WIDTH = 800;
static int HEIGHT = 600;
CentralController centralController;
UserBar user;
DownloaderMenuBar menuBar;//菜单项
public MainFrame() {
this.setTitle("下载器");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int x = screenSize.width / 2 - WIDTH / 2;
int y = screenSize.height / 2 - HEIGHT / 2;
this.setBounds(x, y, WIDTH, HEIGHT);
init();
}
private void init() {
this.setLayout(new BorderLayout());
user = new UserBar();
this.add(user,"North");
}
}
userBar.java
public class UserBar extends JLabel{
private static final long serialVersionUID = 1L;
CentralController centralController;
JPanel panel;
public UserBar(){
init();
}
private void init(){
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,200,150);
panel.setBackground(Color.CYAN);
JLabel label1 = new JLabel();
label1.setBounds(10, 10, 30, 30);
panel.add(label1);
this.add(panel);
label1.setVisible(true);
this.setBorder(BorderFactory.createEtchedBorder(0));
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询