增加所有控件大小和字体大小

时间:2012-07-18 11:55:26

标签: java swing

我在java中创建了一个带有一些基本控件的对话框,我想知道如果参数BIG_DIALOG是,我怎么能将所有对话框控件和字体大小的大小更改为double传递为真。

希望得到一些如何实现这一目标的指导

先谢谢

2 个答案:

答案 0 :(得分:4)

如果您认真对待对话框(并且不影响GUI中的任何其他组件),您将需要递归地遍历内容窗格中的组件层次结构(您开始更高,但让我们让这很容易)

protected void changeContent(Container parent) {
    for (Component comp : parent.getComponents()) {
        adjustFont(comp);
        if (comp instanceof Container) {
            changeContent((Container)comp);
        }
    }
}

protected void changeFont(Component comp) {
    AffineTransformation at = AffineTransformation.getScaleInstance(2, 2);
    Font font = comp.getFont().deriveFont(at);
    comp.setFont(font);
}

public MyDialog() {
    changeContent(getConentPane());
}

当然,您应该能够填充构造函数和参数的参数要求。这是逻辑

<强>更新

首先道歉,我在ipad上输入了原始代码而没有自动完成的优势,因此AffineTransformation应为AffineTransform

我把它放在一起,得到了非常棒的结果。我可以在我自己的代码中保留这个想法,谢谢!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.geom.AffineTransform;

/**
 *
 * @author swhitehead
 * /
public class TestFrame extends javax.swing.JFrame {

    private float scale = 1f;

    /**
     * Creates new form TestFrame    
     */
public TestFrame() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    jPanel1 = new javax.swing.JPanel();
    btnUp = new javax.swing.JButton();
    btnDown = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.GridBagLayout());

    jLabel1.setText("Hello world!");
    getContentPane().add(jLabel1, new java.awt.GridBagConstraints());

    jTextField1.setColumns(12);
    jTextField1.setText("Say something");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
    getContentPane().add(jTextField1, gridBagConstraints);

    jList1.setModel(new javax.swing.AbstractListModel() {
        String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });
    jScrollPane1.setViewportView(jList1);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    getContentPane().add(jScrollPane1, gridBagConstraints);

    jPanel1.setLayout(new java.awt.GridBagLayout());

    btnUp.setText("Up");
    btnUp.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnUpActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    jPanel1.add(btnUp, gridBagConstraints);

    btnDown.setText("Down");
    btnDown.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDownActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    jPanel1.add(btnDown, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 2;
    getContentPane().add(jPanel1, gridBagConstraints);

    pack();
}// </editor-fold>

private void btnUpActionPerformed(java.awt.event.ActionEvent evt) {

    scale += 0.1;
    changeContent(getContentPane());

    invalidate();
    repaint();

}

private void btnDownActionPerformed(java.awt.event.ActionEvent evt) {

    scale -= 0.1;
    if(scale <= 0) scale = 0.1f;

    changeContent(getContentPane());

    invalidate();
    repaint();

}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new TestFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton btnDown;
private javax.swing.JButton btnUp;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

protected void changeContent(Container parent) {
    for (Component comp : parent.getComponents()) {
        adjustFont(comp);
        if (comp instanceof Container) {
            changeContent((Container) comp);
        }
    }
}

protected void adjustFont(Component comp) {
    AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
    Font font = comp.getFont();
    System.out.println("Font.orignal = " + font.getSize());
    font = font.deriveFont(at);
    System.out.println("Font.scaled = " + font.getSize());
    comp.setFont(font);
}
}

我注意到的一件事是Font.original和Font.scaled的输出是相同的。转换发生在绘制阶段,不影响基线字体,只是它的绘图矩阵

答案 1 :(得分:0)

您可以尝试"large" sizeVariant,就像他们展示here一样。