Java 如何计算HTML格式JComponent的维度?
我正在使用Netbeans的GUI构建器设计一个Swing表单,并使用HTML对Java 如何计算HTML格式JComponent的维度?,java,swing,user-interface,jradiobutton,preferredsize,Java,Swing,User Interface,Jradiobutton,Preferredsize,我正在使用Netbeans的GUI构建器设计一个Swing表单,并使用HTML对JRadioButton进行文字包装。这是可行的,但是现在封装的JFrame不会调整JFrame的大小以适应内容。我正在使用默认的GUI生成器的布局管理器,GroupLayout 我试着计算组件被HTML包装后的高度。这是可行的(请参见下面的代码),但在设置了JRadioButton的大小和首选大小后,窗口的大小不会调整 我怎样才能修好它 private void recalculateSize(JComponent
JRadioButton
进行文字包装。这是可行的,但是现在封装的JFrame
不会调整JFrame的大小以适应内容。我正在使用默认的GUI生成器的布局管理器,GroupLayout
我试着计算组件被HTML包装后的高度。这是可行的(请参见下面的代码),但在设置了JRadioButton
的大小和首选大小后,窗口的大小不会调整
我怎样才能修好它
private void recalculateSize(JComponent component, int width) {
View v = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
float preferredSpan = v.getPreferredSpan(View.Y_AXIS);
int height = component.getHeight();
float margin = height - preferredSpan;
Dimension actualSize = getActualSize(component, width);
int actualHeight = (int) (actualSize.height + margin);
Dimension d = new Dimension(component.getPreferredSize().width, actualHeight);
component.setPreferredSize(d);
component.setSize(d);
// This height is the real height. I tested it setting the background
// color to red.
System.out.println("Actual height: " + actualHeight);
}
private static Dimension getActualSize(JComponent component, int width) {
View view = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}
下面是整个代码。由于它是由GUI构建器生成的,因此很难删除不必要的代码
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.text.View;
public class Test extends javax.swing.JFrame {
Test() {
initComponents();
initMoreComponents();
}
private void initComponents() {
one = new javax.swing.ButtonGroup();
two = new javax.swing.ButtonGroup();
three = new javax.swing.JTextField();
four = new javax.swing.JLabel();
five = new javax.swing.JLabel();
six = new javax.swing.JTextField();
seven = new javax.swing.JRadioButton();
eight = new javax.swing.JRadioButton();
nine = new javax.swing.JLabel();
ten = new javax.swing.JLabel();
eleven = new javax.swing.JCheckBox();
twelve = new javax.swing.JCheckBox();
thirteen = new javax.swing.JCheckBox();
fourteen = new javax.swing.JCheckBox();
fifteen = new javax.swing.JCheckBox();
sixteen = new javax.swing.JCheckBox();
seventeen = new javax.swing.JButton();
eightteen = new javax.swing.JButton();
nineteen = new javax.swing.JButton();
twenty = new javax.swing.JRadioButton();
twentyone = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(300, 420));
setResizable(false);
setSize(new java.awt.Dimension(0, 0));
four.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
four.setText("Four");
five.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
five.setText("Five");
one.add(seven);
seven.setSelected(true);
seven.setText("<html>A very long sentence, which might take up more than one line.</html>");
one.add(eight);
eight.setText("<html>Another multiline sentence.</html>");
nine.setText("Nine");
ten.setText("Ten");
eleven.setText("Eleven");
twelve.setText("Twelve");
thirteen.setText("Thirteen");
fourteen.setText("Fourteen");
fifteen.setText("Fifteen");
sixteen.setText("Sixteen");
seventeen.setText("Seventeen");
eightteen.setText("Eightteen");
nineteen.setText("Nineteen");
two.add(twenty);
twenty.setText("<html>Twenty with another line of text</html>");
two.add(twentyone);
twentyone.setText("Twenty one");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(three)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seventeen))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(six)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eightteen))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(nineteen))
.addComponent(eight, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nine)
.addComponent(four)
.addComponent(five)
.addComponent(ten)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(fourteen)
.addComponent(fifteen))
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(twelve)
.addComponent(thirteen))))
.addGroup(layout.createSequentialGroup()
.addComponent(eleven)
.addGap(67, 67, 67)
.addComponent(sixteen)))
.addGap(0, 101, Short.MAX_VALUE))
.addComponent(seven, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(twenty)
.addComponent(twentyone, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(nine)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(twenty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(twentyone)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(four)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(seventeen))
.addGap(18, 18, 18)
.addComponent(five)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(eightteen))
.addGap(18, 18, 18)
.addComponent(ten)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(eleven)
.addComponent(sixteen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fourteen)
.addComponent(thirteen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fifteen)
.addComponent(twelve))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nineteen)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void initMoreComponents() {
recalculateSize(this.seven, 280);
}
private void recalculateSize(JComponent component, int width) {
View v = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
float preferredSpan = v.getPreferredSpan(View.Y_AXIS);
int height = component.getHeight();
float margin = height - preferredSpan;
Dimension actualSize = getActualSize(component, width);
int actualHeight = (int) (actualSize.height + margin);
Dimension d = new Dimension(component.getPreferredSize().width, actualHeight);
component.setPreferredSize(d);
component.setSize(d);
component.revalidate();
System.out.println("Actual height: " + actualHeight);
System.out.println("Dimension preferred and set size: " + d);
System.out.println("Dimension after revalidate: " + component.getPreferredSize());
pack();
}
private static Dimension getActualSize(JComponent component, int width) {
View view = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}
public static void main(String args[]) {
new Test().setVisible(true);
}
private javax.swing.JCheckBox eleven;
private javax.swing.JCheckBox fourteen;
private javax.swing.JButton eightteen;
private javax.swing.JButton seventeen;
javax.swing.JTextField six;
private javax.swing.JLabel five;
private javax.swing.JRadioButton twentyone;
private javax.swing.JCheckBox fifteen;
private javax.swing.JButton nineteen;
private javax.swing.JRadioButton seven;
private javax.swing.ButtonGroup one;
private javax.swing.JLabel nine;
private javax.swing.JRadioButton twenty;
private javax.swing.ButtonGroup two;
private javax.swing.JRadioButton eight;
javax.swing.JTextField three;
private javax.swing.JLabel four;
private javax.swing.JCheckBox sixteen;
private javax.swing.JCheckBox thirteen;
private javax.swing.JCheckBox twelve;
private javax.swing.JLabel ten;
}
导入java.awt.Dimension;
导入javax.swing.JComponent;
导入javax.swing.text.View;
公共类测试扩展了javax.swing.JFrame{
测试(){
初始化组件();
initmore组件();
}
私有组件(){
one=newjavax.swing.ButtonGroup();
two=newjavax.swing.ButtonGroup();
三=新的javax.swing.JTextField();
four=newjavax.swing.JLabel();
five=newjavax.swing.JLabel();
six=newjavax.swing.JTextField();
seven=newjavax.swing.JRadioButton();
八=新的javax.swing.JRadioButton();
nine=newjavax.swing.JLabel();
ten=newjavax.swing.JLabel();
十一=新的javax.swing.JCheckBox();
十二=新的javax.swing.JCheckBox();
十三=新的javax.swing.JCheckBox();
十四=新的javax.swing.JCheckBox();
十五=新的javax.swing.JCheckBox();
十六=新的javax.swing.JCheckBox();
十七=新javax.swing.JButton();
eightteen=newjavax.swing.JButton();
十九=新的javax.swing.JButton();
二十=新的javax.swing.JRadioButton();
twentyone=newjavax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(新的java.awt.Dimension(300420));
可设置大小(假);
setSize(新java.awt.Dimension(0,0));
setFont(新的java.awt.Font(“Tahoma”,1,11));//NOI18N
四.setText(“四”);
setFont(新的java.awt.Font(“Tahoma”,1,11));//NOI18N
五.setText(“五”);
一.加入(七);
七、当选(对);
七、setText(“一个很长的句子,可能要用一行以上的文字。”);
一.增加(八);
8.setText(“另一个多行句子”);
九.setText(“九”);
十.setText(“十”);
11.setText(“十一”);
十二.setText(“十二”);
十三、setText(“十三”);
十四、setText(“十四”);
十五。setText(“十五”);
十六.setText(“十六”);
十七、setText(“十七”);
八分之一。八分之一(“八分之一”);
十九.setText(“十九”);
二.加上(二十);
二十.setText(“二十加另一行文字”);
二.加上(二十一);
二十一。setText(“二十一”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.training)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
.addComponent(三个)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(十七)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
.addComponent(六)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(八分之一))
.addGroup(layout.createSequentialGroup()
.addGap(0,0,短.MAX_值)
.addComponent(十九)
.addComponent(八,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.PREFERRED\u SIZE,0,Short.MAX\u值)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(九)
.addComponent(四个)
.addComponent(五个)
.addComponent(十个)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(十四)
.ADD部分(十五)
.addGap(87,87,87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(十二)
.addComponent(十三)
.addGroup(layout.createSequentialGroup()
.addComponent(十一)
.addGap(67,67,67)
.addComponent(十六)
.addGap(0,101,短。最大值))
.addComponent(seven,javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.cre