0

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

希望获得有关如何完成此任务的一些指导

提前致谢

4

2 回答 2

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 的输出是相同的。转换发生在绘制阶段,不会影响基线字体,只是绘制矩阵

于 2012-07-18T14:42:35.353 回答
0

你可以试试"large" sizeVariant他们在这里展示的。

于 2012-07-18T14:46:16.260 回答