我正在编写一个程序,我想在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);
}
}
我想在JTabbedPane的右上角添加按钮和标签。
您可以通过适当布局嵌套Container
来将Components
添加到JTabbedPane
之上。例如,要将JLabel
和JButton
放置在JTabbedPane的上方和右上方,您可以在添加了JTabbedPane的mouseClicked
实现中使用BoxLayout:
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
)来满足您的要求。