我想在JFrame中应用渐变。 我尽我所能,但遇到了一些问题。 请帮帮我。
在以下代码中。 我想将pnlMain设置为BorderLayout,但我使用了渐变绘画,因此未正确设置BorderLayout。 而且graphics2d.fillRect(0,0,width,height)也没有填满整个pnlMain JPanel。 所以请给出一些建议。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Frame extends javax.swing.JFrame {
// Getter & Setter
public Frame() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setTitle("Gradient");
initComponents();
}
private void initComponents() {
menu_bar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
pnlMain = new JPanel();
pnlSuper = new JPanel(new BorderLayout(15,15))
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Point point1 = new Point(10, 10);
Point point2 = new Point(
getWidth() - 10,
getHeight() - 10);
final GradientPaint gp = new GradientPaint(
point1, Color.YELLOW,
point2, new Color(255, 225, 100),
true);
final Graphics2D g2 = (Graphics2D) g;
g2.setPaint(gp);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
pnlSuper.setBorder(new EmptyBorder(30,30,30,30));
pnlMain.setLayout(new BorderLayout());
pnlMain.setOpaque(false);
pnlImage = new JPanel(new BorderLayout());
pnlFooter = new JPanel(new GridLayout(1,2));
footerBox = Box.createVerticalBox();
lblBackground = new JLabel("Image");
lblBackground.setFont(new java.awt.Font("URW Bookman L", 0, 20));
lblAddress = new JLabel();
lblContactNumber = new JLabel();
lblBackground.setHorizontalAlignment(SwingConstants.CENTER);
lblAddress.setText("Address");
lblAddress.setAlignmentX(JLabel.CENTER_ALIGNMENT);
lblAddress.setFont(new java.awt.Font("URW Bookman L", 0, 20)); // NOI18N
footerBox.add(lblAddress);
lblContactNumber.setText("Contact Number ");
lblContactNumber.setFont(new java.awt.Font("URW Bookman L", 0, 20)); // NOI18N
lblContactNumber.setAlignmentX(JLabel.CENTER_ALIGNMENT);
footerBox.add(lblContactNumber);
pnlFooter.add(footerBox);
pnlImage.add(lblBackground);
pnlMain.add(pnlImage, BorderLayout.CENTER);
pnlMain.add(pnlFooter, BorderLayout.SOUTH);
pnlSuper.add(pnlMain);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("File");
jMenu1.setFont(new java.awt.Font("URW Bookman L", 0, 18));
jMenu1.setPreferredSize(new java.awt.Dimension(45, 25));
menu_bar.add(jMenu1);
setJMenuBar(menu_bar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addGap(0, 1352, Short.MAX_VALUE).addComponent(pnlSuper));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGap(0, 757, Short.MAX_VALUE).addComponent(pnlSuper));
pack();
}
public static void main(String args[]) {
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(Index.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true);
}
});
}
private javax.swing.JMenu jMenu1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JMenuBar menu_bar;
private JLabel lblBackground;
private JPanel pnlMain;
private JPanel pnlFooter;
private JPanel pnlImage;
private JLabel lblAddress;
private JLabel lblContactNumber;
private Box footerBox;
private JPanel pnlSuper;
}
Thanq For Reading
答案 0 :(得分:2)
要查看BG,全部,其顶部的组件必须是透明的。所以这个:
pnlImage = new JPanel(new BorderLayout());
pnlFooter = new JPanel(new GridLayout(1,2));
应该是:
pnlImage = new JPanel(new BorderLayout());
pnlImage.setOpaque(false);
pnlFooter = new JPanel(new GridLayout(1,2));
pnlFooter.setOpaque(false);