如何使窗体自适应分辨率java swing

               

from:http://wenda.tianya.cn/wenda/thread?tid=29ce007bd6e974ee&from=bottomThread


总共两个文件,第一个是启动文件,第二个是一个基础面板类,可以运行。已经帮你排好版面了,只是未有添加事件响应,你研究一下自己添加吧



package com;

import com.ren.BasePanel;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Toolkit;

public class SwingTest extends JFrame {
   public static void main(String args[]) {
       new SwingTest();
   }

   public SwingTest() {
       super("SwingTextField 测试");
       init();
       setup();
       this.setSize(800, 300);
       this.setVisible(true);
       this.setLocation
               ( //定位框架位置
                       (int) (Toolkit.getDefaultToolkit().getScreenSize().
                              getWidth() - 400) / 2,
                       (int) (Toolkit.getDefaultToolkit().getScreenSize().
                              getHeight() - 300) / 2
               );
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   BasePanel leftPanel;
   BasePanel rightPanel;
   JTextField country = new JTextField(16);
   JButton search = new JButton("SEARCH");
   JTextField latitude = new JTextField(40);
   JTextField currentTime = new JTextField(40);
   JTextField wind = new JTextField(40);
   JTextField visibilityField = new JTextField(40);
   JTextField skycondition = new JTextField(40);
   JTextField dewpoint = new JTextField(40);
   JTextField relativehumidity = new JTextField(40);
   JTextField presure = new JTextField(40);

   public void init() {
       leftPanel = new BasePanel() {
           public void initAllComponents() {
           }

           public void layoutAllComponents() {
               addComponent(country, 0, 0, 1, 1, 10, 10);
               addComponent(search, 1, 0, 1, 1, 10, 10);
           }
       };

       rightPanel = new BasePanel() {
           public void initAllComponents() {
           }

           public void layoutAllComponents() {
               addComponent(latitude, 0, 0, 1, 1, 10, 10);
               addComponent(currentTime, 1, 0, 1, 1, 10, 10);
               addComponent(wind, 2, 0, 1, 1, 10, 10);
               addComponent(visibilityField, 3, 0, 1, 1, 10, 10);
               addComponent(skycondition, 4, 0, 1, 1, 10, 10);
               addComponent(dewpoint, 5, 0, 1, 1, 10, 10);
               addComponent(relativehumidity, 6, 0, 1, 1, 10, 10);
               addComponent(presure, 7, 0, 1, 1, 10, 10);
           }
       };
   }

   public void setup() {
       this.setLayout(new BorderLayout());
       this.add(leftPanel, BorderLayout.WEST);
       this.add(rightPanel, BorderLayout.EAST);
   }
}

以下是我以前的经验总结,比较有用的一个类,你可以好好研究一下
/**<p>
* =============================================================================
* <p>Copyright (c) 2008, Ren Java Studio
* <p>All rights reserved.<p>
* =============================================================================
* <p>文件名称:BasePanel.java
* <p>文件标识:见配置管理计划书
* <p>摘    要:抽象基础面板
* @version 2.0
* @author Ren
* <p>完成日期:2008年5月5日.<p>
=============================================================================*/
package com.ren;

import java.awt.*;
import javax.swing.*;

public abstract class BasePanel extends JPanel {
   /*当前面板的容器*/
   protected Container container;
   /*网格布局器*/
   protected GridBagLayout layout;
   /*网格布局器的约束器*/
   protected GridBagConstraints constraints;

   public BasePanel() {
       container = this;
       layout = new GridBagLayout();
       constraints = new GridBagConstraints();
       container.setLayout(layout);
       constraints.fill = constraints.BOTH; //在水平方向和垂直方向上同时调整组件大小
       initAllComponents();
       layoutAllComponents();
   }

   /**<p>
    * =========================================================================
    * <p>初始化所有GUI组件.<p>
    =========================================================================*/
   protected abstract void initAllComponents();

   /**@todo 继承之子类完成所有组件的初始化工作 */

   /**<p>
    * ===================================
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值