rwa*_*ace 1 java forms layout swing
我正在编写一个Java Swing程序,需要让用户从"设置"表单中选择8种不同的颜色.就JColorChooser而言,这是非常简单的,但我想知道是否有推荐的方式来布置表单.这就是我目前正在考虑的事情:
对于每种颜色,
以当前所选颜色显示正方形,
在其右侧显示一个按钮,该按钮将调出JColorChooser以更改所选颜色.
但是,我怀疑8个按钮会使表格看起来很笨重,如果不是非常丑陋(特别是如果以后添加更多).另一种方法可能是将方形双作为按钮; 在这种情况下,如何直观地表明它是可点击的?
有替代的推荐方法吗?
这是ColorButton
我在pscode API中使用的.
package org.pscode.ui.widget;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
/** Provides a button for popping a color chooser.
The Color can be obtained by calling ColorButton.getBackground().
@author Andrew Thompson. */
public class ColorButton extends JButton implements ActionListener {
private JColorChooser chooseColor;
private String htmlColor;
private String text;
public ColorButton(String label) {
super(label);
text = label;
chooseColor = new JColorChooser();
addActionListener( this );
}
@Override
public void setText(String text) {
super.setText(
"<html><body style='text-align: left;'><center>" +
"<p>" +
text +
"<br>" +
"<span style='width: 5px; height: 5px; " +
"border: solid 1px black; background-color: " +
htmlColor +
"; color: " +
htmlColor +
";'>AA</span> " +
"</center></body></html>");
this.text = text;
}
public void actionPerformed(ActionEvent ae) {
Color c = chooseColor.showDialog(
this,
"Choose Color",
getBackground() );
if (c!=null) {
setBackground(c);
}
}
@Override
public void setBackground(Color bg) {
super.setBackground( new Color(bg.getRed(), bg.getGreen(), bg.getBlue()) );
int r = bg.getRed();
int g = bg.getGreen();
int b = bg.getBlue();
htmlColor = "rgb(" + r + "," + g + "," + b + ")";
setText(text);
}
}
Run Code Online (Sandbox Code Playgroud)
典型用法的屏幕截图.
@rwallace:你是怎么让他们成为相同的形状(无论标题的长度)..
布局的那部分(对于5个按钮和复选框)使用的是GridLayout
.您还可以通过以下方式更改按钮的大小:
body
在HTML 的元素的样式中添加一些填充."......还有略圆的角落和闪亮的效果?"
DukeBox(从中获取截图)使用原生PLAF - 我正在运行Windows 7.
归档时间: |
|
查看次数: |
414 次 |
最近记录: |