Java中怎么让标签在容器中居中
importjavax.swing.*;importjava.awt.*;publicclasspanelcenterextendsJFrame{/***@paramar...
import javax.swing.*;
import java.awt.*;public class panelcenter extends JFrame { /**
* @param args
*/
JPanel jp = new JPanel();
public panelcenter(){
this.setLayout(new BorderLayout());
JLabel j1 = new JLabel();
j1.setText("第一个swing程序");
jp.add(j1,BorderLayout.CENTER);
this.add(jp);
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setVisible(true); }
public static void main(String[] args) {
// TODO Auto-generated method stub
new panelcenter();
}} 展开
import java.awt.*;public class panelcenter extends JFrame { /**
* @param args
*/
JPanel jp = new JPanel();
public panelcenter(){
this.setLayout(new BorderLayout());
JLabel j1 = new JLabel();
j1.setText("第一个swing程序");
jp.add(j1,BorderLayout.CENTER);
this.add(jp);
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setVisible(true); }
public static void main(String[] args) {
// TODO Auto-generated method stub
new panelcenter();
}} 展开
2个回答
2013-05-16
展开全部
增加两句话,我用粗体标出了:
public panelcenter() {
this.setLayout(new BorderLayout());
JLabel j1 = new JLabel();
j1.setHorizontalAlignment(SwingConstants.CENTER);
j1.setText("第一个swing程序");
jp.setLayout(new BorderLayout());
jp.add(j1, BorderLayout.CENTER);
this.add(jp);
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public panelcenter() {
this.setLayout(new BorderLayout());
JLabel j1 = new JLabel();
j1.setHorizontalAlignment(SwingConstants.CENTER);
j1.setText("第一个swing程序");
jp.setLayout(new BorderLayout());
jp.add(j1, BorderLayout.CENTER);
this.add(jp);
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询