gpt4 book ai didi

java - 增加所有控件的大小和字体大小

转载 作者:行者123 更新时间:2023-12-01 19:02:56 27 4
gpt4 key购买 nike

我正在用java创建一个对话框,其中包含一些基本控件,我想知道如果参数BIG_DIALOG作为true传递,如何将所有对话框控件的大小和字体大小更改为双倍。

希望获得一些关于如何实现这一目标的指导

提前致谢

最佳答案

如果您真的只想对对话框执行此操作(并且不影响 GUI 中的任何其他组件),则您将需要递归地遍历内容 Pane 中声明的组件层次结构(您可以从更高的位置开始,但让我们这很容易)

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

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

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

当然,您应该能够填写构造函数的参数要求及其逻辑

更新

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

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

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

关于java - 增加所有控件的大小和字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com