我正在处理一个项目,我正在JPanel上绘制图像,然后将该面板放在JScrollPane中。面板本身的尺寸有时会比屏幕小,有时会更大。当它更大时,滚动窗格似乎没有出现任何滚动条。我确定我搞砸了一些简单的东西,但我似乎无法弄明白。
任何帮助都会有很大的帮助。
public void createGI(theTicket[] ticketArray){
JFrame ticketReportFrame = new JFrame();
ticketReportFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel ticketReport = new drawTurnoverReport(ticketArray , notes, tapes, turnover);
ticketReport.setBackground(Color.WHITE);
ticketReport.setLayout(new BorderLayout());
JScrollPane ticketReportHolder = new JScrollPane(ticketReport);
ticketReportFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
ticketReportFrame.pack();
ticketReportHolder.setWheelScrollingEnabled(true);
ticketReportFrame.add(ticketReportHolder);
ticketReportFrame.setVisible(true);
ticketReportFrame.setResizable(false);
}
这只是drawTurnoverReport的一小部分,试图帮助展示我在做什么。
public class drawTurnoverReport extends JPanel{
public drawTurnoverReport(theTicket[] tickets, String[] n, String[] t, String[] to){
ticArray = tickets;
notes = n;
tapes = t;
turnover = to;
formalDate = mt.formalDate;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent( g );
Graphics2D g2 = (Graphics2D) g;
//set fonts
Font tahomaB = new Font("tahoma", Font.BOLD, 15);
Font tahomaP = new Font("tahoma", Font.PLAIN, 15);
//draw heading
g2.setColor(Color.BLACK);
g2.setFont(tahomaB);
Rectangle2D rec = g2.getFontMetrics().getStringBounds("Turnover / Activity Log", g2);
int stringW = (int) rec.getWidth();
int prevStringH = (int) rec.getHeight();
g2.drawString("Turnover / Activity Log", x-(stringW / 2) , 50);
g2.setFont(tahomaP);
rec = g2.getFontMetrics().getStringBounds("Date: " + formalDate, g2);
stringW = (int) rec.getWidth();
verticalPlace = 50 + prevStringH;
g2.drawString("Date: " + formalDate, x-(stringW / 2) , verticalPlace);
prevStringH = (int) rec.getHeight();
verticalPlace += prevStringH;
g2.setFont(tahomaB);
rec = g2.getFontMetrics().getStringBounds("Service Manager Tickets", g2);
stringW = (int) rec.getWidth();
g2.drawString("Service Manager Tickets", x-(stringW / 2) , verticalPlace);
verticalPlace +=2;
g2.drawLine(((int)rec.getMinX()+ x)-(stringW / 2 +2), verticalPlace,
((int)rec.getMaxX()+x)-(stringW / 2), verticalPlace);
verticalPlace += 40;
//draw table headers
verticalUnderHeader = verticalPlace;
g2.drawLine(50, verticalPlace, w-50, verticalPlace);
rec = g2.getFontMetrics().getStringBounds(ticArray[0].getTicketNum(), g2);
stringW = (int) rec.getWidth();
rec = g2.getFontMetrics().getStringBounds("Ticket #", g2);
temp = (int) rec.getWidth();
g2.drawString("Ticket #", ((stringW+65+50)/2-(temp/2)), verticalPlace+17);
horzPlace = stringW + 65;
CNCxA = horzPlace;
rec = g2.getFontMetrics().getStringBounds("Customer Name", g2);
stringW = (int) rec.getWidth();
widthCNC = (horzPlace + stringW + 30)-horzPlace;
g2.drawString("Customer Name", horzPlace + 15, verticalPlace+17);
horzPlace = horzPlace + stringW + 30;
CNCxB = horzPlace;
temp = w-horzPlace+30;
DCx = temp;
widthDC = temp - horzPlace;
rec = g2.getFontMetrics().getStringBounds("Description", g2);
stringW = (int) rec.getWidth();
g2.drawString("Description", (horzPlace+temp)/2-(stringW/2), verticalPlace+17);
rec = g2.getFontMetrics().getStringBounds("Status", g2);
stringW = (int) rec.getWidth();
g2.drawString("Status", (temp+(w-50))/2-(stringW/2), verticalPlace+17);
verticalPlace += 20;
g2.drawLine(50, verticalPlace, w-50, verticalPlace);
verticalPlaceTemp = verticalPlace + 20;
}
}
答案 0 :(得分:2)
从事物的声音来看,drawTurnoverReport
组件未向滚动窗格提供足够大小的提示信息。
JScrollPane
将使用组件preferredSize
来确定是否需要显示滚动条。
确保覆盖getPreferredSize
方法并返回适当大小的组件。
例如:
这假设您正在执行某种自定义绘制,而不使用JLabel
之类的内容来渲染图像,这会建议您在某处出现布局问题...但没有{{1 ,不可能知道...