TitledBorder 设置JPanel边框

JPanel的边框
Border picBorder = BorderFactory.createTitledBorder("图片展示");
picPanel.setBorder(picBorder);
当然还有方法重载,可以设颜色。
setBorder(new TitledBorder(null, "标题", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.BLUE));

TitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont, Color titleColor) 

用指定的边框、标题、标题对齐方式、标题位置、标题字体和标题颜色创建 TitledBorder 实例
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class ColorTest extends JFrame { 
 private static final long serialVersionUID = 1L;

 /**
  * Launch the application
  * @param args
  */
 public static void main(String args[]) {
  try {
   ColorTest frame = new ColorTest();
   frame.setVisible(true);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * Create the frame
  */
 public ColorTest() {
  super();
  getContentPane().setLayout(null);
  setBounds(100, 100, 500, 375);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  final JPanel panel = new JPanel();
  panel.setBorder(new TitledBorder(null, "标题", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.BLUE));
  panel.setLayout(null);
  panel.setBounds(103, 45, 274, 174);
  getContentPane().add(panel);

  final JButton button = new JButton();
  button.setBounds(84, 74, 106, 28);
  button.setText("New JButton");
  panel.add(button);
  //
 }

}


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Java Swing库中的布局管理器来实现将按钮并排排列的效果。以下是修改后的示例代码: ``` import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonExample extends JFrame { public ButtonExample() { setTitle("Button Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 200); // 创建按钮和布局管理器 JToggleButton searchUserButton = new JToggleButton("搜索新用户"); JButton stopSearchButton = new JButton("终止搜索"); FlowLayout layout = new FlowLayout(); // 创建搜索用户面板 JPanel searchUserPanel = new JPanel(); searchUserPanel.setBorder(new TitledBorder("搜索用户")); // 创建搜索控制面板 JPanel searchControlPanel = new JPanel(); searchControlPanel.setLayout(layout); searchControlPanel.add(searchUserButton); searchControlPanel.add(stopSearchButton); // 将搜索控制面板添加到搜索用户面板 searchUserPanel.add(searchControlPanel); // 添加进度条到搜索控制面板 JProgressBar progressBar = new JProgressBar(); searchControlPanel.add(progressBar); // 点击终止搜索按钮后,调用SearchUserActionListener中的stopSearch方法 stopSearchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ActionListener[] actionListeners = searchUserButton.getActionListeners(); if (actionListeners.length > 0 && actionListeners[0] instanceof SearchUserActionListener) { ((SearchUserActionListener) actionListeners[0]).stopSearch(); } } }); // 添加搜索用户面板到窗口 add(searchUserPanel); setVisible(true); } public static void main(String[] args) { new ButtonExample(); } } ``` 在这个示例中,我们使用FlowLayout布局管理器,并将搜索控制面板中的搜索新用户按钮和终止搜索按钮添加到同一个布局中,这样它们就可以并排排列了。我们还将搜索控制面板添加到了搜索用户面板中,然后将搜索用户面板添加到了窗口中。点击终止搜索按钮后,我们获取搜索新用户按钮中的ActionListener,并调用其中的stopSearch方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值