我在“JLabel”中有一席:
是否有可能在彩色icon和案文之间添加另一个icon(例如代表一国的国旗)? 例如,我要补充一条一条,说明红 i和<条码>之间的国旗。 US 。 感谢!
我在“JLabel”中有一席:
是否有可能在彩色icon和案文之间添加另一个icon(例如代表一国的国旗)? 例如,我要补充一条一条,说明红 i和<条码>之间的国旗。 US 。 感谢!
是的,在集装箱标签上使用带盒式的JLabel:
JLabel container = new JLabel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JLabel icon1Label = new JLabel();
JLabel icon2Label = new JLabel();
icon1Label.setIcon(icon1);
icon2Label.setIcon(icon2);
container.add(icon1Label);
container.add(icon2Label);
Try CompoundIcon Edit: Heisenbug s layout-based solution is the trivial answer.
我只是最近才这样做了——我希望能够把单行的多个电灯合并为一个单行。 我这样做的方式是,将两个图像按手写成单一图像,然后作为我的<条码>。 还有其他实现同样效果的办法,但我确实不想改变我的核心要素。
我最后创建了一个组合多种图像和藏匿的班子。 我这样说:
ImageIcon icon1 = ...;
ImageIcon icon2 = ...;
ImageIcon labelIcon = new CachedCompositeIcon( icon1, icon2 ).getIcon();
jLabel.setIcon( labelIcon );
来源:
/** This is a convenience class to handle creating a single composite icon from several icons, and caching the
* created icons to eliminate duplicate work. This class is basically used as a key into a map, allowing us to
* define both a hashCode and equals in a single place.
*/
public class CachedCompositeIcon
{
private static final byte ICON_PADDING = 2;
private static final HashMap<CachedCompositeIcon, ImageIcon> CACHED_ICONS =
new HashMap<CachedCompositeIcon, ImageIcon>( 4 );
private final ImageIcon[] m_icons;
public CachedCompositeIcon(final ImageIcon... icons) {
m_icons = icons;
}
public ImageIcon getIcon() {
if ( !CACHED_ICONS.containsKey( this ) ) {
CACHED_ICONS.put( this, lcl_combineIcons() );
}
return CACHED_ICONS.get( this );
}
/** Generates an icon that is a composition of several icons by appending each icon together with some
* padding between them.
*
* @return An icon that is the concatenation of all the icons this was constructed with.
*/
private ImageIcon lcl_combineIcons() {
// First determine how big our composite icon will be; we need to know how wide & tall to make it.
int totalWidth = (m_icons.length - 1) * ICON_PADDING; // Take into account the padding between icons
int minHeight = 0;
for ( int i = 0; i < m_icons.length; ++i ) {
totalWidth += m_icons[i].getIconWidth();
if ( m_icons[i].getIconHeight() > minHeight ) {
minHeight = m_icons[i].getIconHeight();
}
}
// Create an image big enough and acquire the image canvas to draw on
final BufferedImage compositeImage = new BufferedImage( totalWidth, minHeight, BufferedImage.TYPE_INT_ARGB );
final Graphics graphics = compositeImage.createGraphics();
// Iterate over the icons, painting each icon and adding some padding space between them
int x = 0;
for ( int i = 0; i < m_icons.length; ++i ) {
final ImageIcon icon = m_icons[ i ];
graphics.drawImage( icon.getImage(), x, 0, null );
x += icon.getIconWidth() + ICON_PADDING;
}
return new ImageIcon( compositeImage );
}
/** Generates a hash that takes into account the number of icons this composition includes and the hash &
* order of those icons.
*
* @return A hash code.
*/
@Override
public int hashCode() {
int weakHash = m_icons.length;
for ( int i = 0; i < m_icons.length; ++i ) {
weakHash += m_icons[i].hashCode() * (i + 1);
}
return weakHash;
}
/** Two instances are equal if and only if they include the same icons and they re in the same order.
*
* @param obj An object to check for equality with this.
*
* @return true if the two objects are equal, false otherwise.
*/
@Override
public boolean equals(final Object obj) {
if ( !(obj instanceof CachedCompositeIcon) ) {
return false;
}
final CachedCompositeIcon other = (CachedCompositeIcon) obj;
if ( m_icons.length != other.m_icons.length ) {
return false;
}
for ( int i = 0; i < m_icons.length; ++i ) {
if ( m_icons[i].hashCode() != other.m_icons[i].hashCode() ) {
return false;
}
}
return true;
}
}
这样做是完全可能的,JLabel正在上任,你可以向另一名JComponent增加任何JComponent,而JLabel的情况也是如此。
label.setLayout(new GridLayout(0, 2, 10, 10));
label.add(myIcon1);
label.add(myIcon2);
if you add f.e. JPanel to the JLabel then don t forget to setOpaque(false);
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...