随便写写——JFrame中加载JPanel,JLabel中字符串换行显示

来源:互联网 发布:软件开发流程图工具 编辑:程序博客网 时间:2024/06/05 21:54
 

public class JPanelJFrame extends JFrame {

 HelpPanel panel = new HelpPanel();
 
 public JPanelJFrame() {
  this.setLayout(new BorderLayout());
  this.getContentPane().add(panel);
  
  this.setSize(panel.getWidth() + 10, panel.getHeight() + 10);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);
 }
 public static void main(String[] args) {
  new  JPanelJFrame();

 }

}

 

public class HelpPanel extends JPanel {
 private JLabel loadMIBLabel;
 private JLabel loadMIBPicLabel;
 private JLabel loadDataLabel;
 private JLabel loadDataPicLabel;
 
 private int w_width;
 private int h_height;
 
 private int maxWidth;
 
 private String picPath = "images/splash.jpg";
 public HelpPanel() {
  initGUI();
 }

 private void initGUI() {
  try {
   {
    GridBagLayout thisLayout = new GridBagLayout();
    thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
    thisLayout.rowHeights = new int[] {7, 7, 7, 7, 7, 7, 7, 7};
    thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};  
    thisLayout.columnWidths = new int[] {7, 7, 7, 7, 7, 7, 7, 7};
    this.setLayout(thisLayout);
    {
     loadMIBLabel = new JLabel("<html>加载文件的步骤是:<br> 1.选择'文件'菜单,然后选择'加载文件'菜单项 <br>2.在弹出的'加载文件对话框'中,输入文件的绝对路径,或者点击'浏览'按钮,选择文件</html>", SwingConstants.CENTER);
     this.add(loadMIBLabel, new GridBagConstraints(0, 0, 8, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 12, 6, 12), 0, 0));
//     loadMIBLabel.setHorizontalTextPosition(JLabel.CENTER);
     System.out.println(loadMIBLabel.getWidth());
     maxWidth = maxWidth > loadMIBLabel.getWidth() ? maxWidth : loadMIBLabel.getWidth();
     h_height += 100;
    }
    {
     ImageIcon imageIcon = new ImageIcon(picPath);
     Image image = imageIcon.getImage();
     int width = image.getWidth(null);
     int height = image.getHeight(null);
     loadMIBPicLabel = new JLabel(imageIcon);
     this.add(loadMIBPicLabel, new GridBagConstraints(0, 1, 8, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 12, 6, 12), 0, 0));
//     loadMIBPicLabel.setText("<html>步骤是:<br> 1.打开菜单 <br>2.选择文件</html>");
     maxWidth = maxWidth > width ? maxWidth : width;
     h_height += height;
    }
    {
     loadDataLabel = new JLabel();
     this.add(loadDataLabel, new GridBagConstraints(0, 4, 8, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 12, 6, 12), 0, 0));
     loadDataLabel.setText("<html>步骤是:<br> 1.打开菜单 <br>2.选择文件</html>");
     maxWidth = maxWidth > loadDataLabel.getWidth() ? maxWidth : loadDataLabel.getWidth();
     h_height += 100;
     
    }
    {
     ImageIcon imageIcon = new ImageIcon(picPath);
     Image image = imageIcon.getImage();
     int width = image.getWidth(null);
     int height = image.getHeight(null);
     loadDataPicLabel = new JLabel(imageIcon);
     this.add(loadDataPicLabel, new GridBagConstraints(0, 5, 8, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 12, 6, 12), 0, 0));
//     loadDataPicLabel.setText("<html>步骤是:<br> 1.打开菜单 <br>2.选择文件</html>");
     maxWidth = maxWidth > width ? maxWidth : width;
     h_height += height;
    }
    {
     this.w_width = maxWidth;
     this.setSize(this.w_width, h_height);
     System.out.println(this.w_width + " " + this.h_height);
    }
    
   }
  } catch(Exception e) {
   e.printStackTrace();
  }
 }

}

 

原创粉丝点击