我正在编写程序,我想在JTabbedPane上方的右上方添加按钮和标签。我使用MatteBorder,我想添加标签(不是标题)和蓝色边框所在的1个按钮。谢谢你的帮助。
import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import static javax.swing.SwingConstants.CENTER;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.swing.event.*;
public class hotels extends JFrame{
JButton hotel;
JLabel image;
JTabbedPane tabbed,tabbed1;
JPanel panel;
JPanel panel1;
Container pane;
JPanel panel2;
JLabel departure;
JLabel from;
JLabel to;
public hotels(){
panel=new JPanel();
panel.setBackground(Color.cyan);
hotel=new JButton();
hotel.setText("Hotels");
Font myFont = new Font("Serif", Font.BOLD, 18);
hotel.setFont(myFont);
panel.setLayout(null);
panel.add(hotel);
hotel.setBounds(50, 80, 100, 40);
image=new JLabel();
image.setBounds(50,1,80,80);
image.setBorder(BorderFactory.createLineBorder(Color.yellow));
image.setBackground(Color.white);
image.setIcon(new ImageIcon("2.gif"));
panel.add(image);
panel1=new JPanel();
panel1.setLayout(null);
tabbed=new JTabbedPane();
tabbed.setBorder(BorderFactory.createMatteBorder(24, 6, 12, 6, Color.blue));
Font myFont1 = new Font("Serif", Font.BOLD, 18);
tabbed.setFont(myFont1);
tabbed.setBounds(0, 20, 80, 40);
tabbed.add( "Round Trip",panel1);
panel2=new JPanel();
panel2.setLayout(null);
panel2.setBackground(Color.white);
departure=new JLabel();
departure.setText("Departure");
Font f=new Font("Serif", Font.PLAIN,12);
departure.setBounds(50,1,80,80);
departure.setFont(f);
panel2.add(departure);
tabbed.add("One Way",panel2);
from=new JLabel();
from.setBackground(Color.blue);
panel1.setBackground(Color.yellow);
pane=getContentPane();
pane.add(panel);
image.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if (e.getSource()==image){
pane.removeAll();
pane.add(tabbed);
pane.revalidate();
pane.repaint();
}
}
}
);
}
public static void main(String[] args) {
hotels mw=new hotels();
mw.setVisible(true);
mw.setSize(300, 400);
}
}
答案 0 :(得分:1)
我想在JTabbedPane上方的右上方添加按钮和标签。
您可以通过使用适当的布局嵌套Components
来在JTabbedPane
上方添加Container
。例如,要在JTabbedPane的上方和右上方放置JLabel
和JButton
,您可以使用BoxLayout - 在mouseClicked
实现中添加JTabbedPane:
pane.removeAll();
Box vert = Box.createVerticalBox();
Box top = Box.createHorizontalBox();
top.add(Box.createHorizontalGlue());
top.add(new JLabel("Top Label"));
top.add(new JButton("Top Button"));
vert.add(top);
vert.add(tabbed);
pane.add(vert);
....
您的要求不明确,因此您可能必须使用背景颜色和边框位置(例如,将边框添加到vert
而不是tabbed
)以满足您的要求。