【发布时间】:2015-01-31 10:14:21
【问题描述】:
代码现已启动并运行。感谢 MadProgrammer 的所有帮助。他是个球手。
基本上,这段代码的主要内容是随机字符、随机大小和随机颜色。
下面是代码:
随机颜色:
int a = (int)(Math.random() * 255 - 0);
int b = (int)(Math.random() * 255 - 0);
int c = (int)(Math.random() * 255 - 0);
int z = (int)(Math.random() * 300 - 0);
Label.setForeground(new java.awt.Color(a, b, c));
随机大小:
Random rand = new Random();
int random = (int)(Math.random() * 100000 - 50000);
Label.setFont(new Font("Courier New", Font.ITALIC, z));
随机文本:
char randomChar = (char)(int)((Math.random() * 93) + 33);
Label.setText(String.valueOf(randomChar));
真实 OG 的完整代码:
package my.dimensional;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Random;
import static javafx.scene.paint.Color.color;
import static javafx.scene.text.Font.font;
import static javafx.scene.text.Font.font;
import javax.swing.JLabel;
public class DimensionalUI extends javax.swing.JFrame
{
public DimensionalUI()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jLabel2 = new javax.swing.JLabel();
Run = new javax.swing.JButton();
Label = new javax.swing.JLabel();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jLabel2.setText("jLabel2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Run.setText("Run");
Run.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RunActionPerformed(evt);
}
});
Label.setText("BOOM");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Run, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(Label, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE)
.addComponent(Run)
.addContainerGap())
);
pack();
}// </editor-fold>
private void RunActionPerformed(java.awt.event.ActionEvent evt) {
Random rand = new Random();
int random = (int)(Math.random() * 100000 - 50000);
int a = (int)(Math.random() * 255 - 0);
int b = (int)(Math.random() * 255 - 0);
int c = (int)(Math.random() * 255 - 0);
int z = (int)(Math.random() * 300 - 0);
Label.setForeground(new java.awt.Color(a, b, c));
Label.setFont(new Font("Courier New", Font.ITALIC, z));
/*String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
int index = (int)(Math.random() * (fontNames.length - 1));
String fontName = fontNames[index];
Font font = new Font(fontName, Font.PLAIN, z);
Label.setFont(new Font(fontName, Font.PLAIN, z));*/
char randomChar = (char)(int)((Math.random() * 93) + 33);
Label.setText(String.valueOf(randomChar));
}
public static void main(String args[])
{
//<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(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.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 DimensionalUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Label;
private javax.swing.JButton Run;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
private void getAvailableFontFamilyNames() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
【问题讨论】:
-
使用
JLabel,生成字符、字体和颜色并应用到JLabel。见How to Use Labels -
好的,所以我将 TextField 切换为 JLabel,随机整数仍然有效。我让这段代码浮动 r = rand.nextFloat();浮动 g = rand.nextFloat();浮动 b = rand.nextFloat();颜色随机颜色 = 新颜色(r,g,b);标签 = 随机颜色;但是它不起作用。
-
JLabel#setBackground,JLabel#setFont? -
谢谢,我编辑了我的代码。它现在具有特定的字体、颜色和大小。我唯一不知道的是如何获得随机字体/颜色/大小和随机字符,因为我现在能做的就是整数。
-
颜色很简单,它只是 0 到 255 之间的三个
ints。字符可以通过使用预定义字符数组并随机化元素索引来完成,或者您生成一个介于 33-126 之间的随机数并将其转换为char,这将为您提供等价的ASCIIchar。可以使用GraphicsEnvironment#getAvailableFontFamilyNames()生成字体,它返回一个String数组,然后您只需在数组中生成一个随机索引并提取字体名称...
标签: java random fonts colors font-size