我在java中创建了一个对话框,其中包含一些基本控件,我想知道如何将所有对话框的控件和字体大小更改为双精度,如果参数BIG_DIALOG被通过为真实。增加所有控件的大小和字体大小
希望得到有关如何提前完成这一
感谢
我在java中创建了一个对话框,其中包含一些基本控件,我想知道如何将所有对话框的控件和字体大小更改为双精度,如果参数BIG_DIALOG被通过为真实。增加所有控件的大小和字体大小
希望得到有关如何提前完成这一
感谢
如果你认真对待这样做只为对话框(&不影响在GUI中任何其他成分)的一些准则,你会需要递归走在组件层次的内容窗格中陈述(你coukd开始走高,但让我们使这个容易)
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());
}
当然,你应该能够填补参数requireme对于构造NTS &它的逻辑
UPDATE
首先道歉,我输入的原代码了对我的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的输出是相同的。转换发生在绘画阶段,不会影响基线字体,只是它的绘制矩阵
怪异的....我实现它...它通过组件,但没有大小变化正在发生:( – Killercode 2012-07-18 15:50:03
尝试添加invalidate/repaint后该电话?我没有测试代码,并会看看,当我得到一个电脑前 – MadProgrammer 2012-07-18 19:57:36
解决不是因为无效...只是添加了一些调整:D – Killercode 2012-07-19 15:51:58
您可以尝试"large"
sizeVariant
,就像它们显示here一样。
你可以显示创建对话框的代码吗? – Kostronor 2012-07-18 12:01:42
只在该对话框或整个应用程序中?无论如何,它不支持核心 - 对于想法,看看JGoodies Looks和内部使用的FontPolicy/FontSet,不确定是否有每窗口支持可用但可能添加(从未尝试过,为什么你想拥有一个单一的对话框脱颖而出?) – kleopatra 2012-07-18 13:16:42
只是在这个对话框... – Killercode 2012-07-18 14:29:19